Input/Output til skjerm

Python sine standard utdatafiler er ikkje tilgjengelge når Python-makroar vert køyrde frå menyen Verktøy → Makroar → Køyr makro. For å visa utdata frå ein modul, må ein bruka Python sin interaktive konsoll. Funksjonar som input(), print(), repr() og str() er tilgjengelege frå Python-skalet.

tip

Pythonmodulen LibreOffice msgbox føreslår ein msgbox()-metode som er vist i eksempelsidene Opprett hendingslyttarar og Oppretta ein dialoghandsamar.


LibreOffice Basic føreslår InnputBoks (), Msgbox () og Print () for inn- og utdata til skjerm. Det finst Python-alternativ anten på LibreOffice API Abstract Windowing Toolkit eller i Python til Basic-funksjonskall. Sistnemnde føreslår ein syntaks som er nokså nær Basic, og brukar ein Python-modul ved sida av ein Basic-modul. API Scripting Framework vert brukt til å utføre funksjonskall mellom språka Basic, Beanshell, JavaScript og Python.

Python syntaks:

MsgBox(txt, buttons=0, title=None)

InputBox(txt, title=None, default=None)

Print(txt)

Eksempel:

>>> import screen_io as ui

>>> reply = ui.InputBox('Please enter a phrase', title='Dear user', defaultValue="here..")

>>> rc = ui.MsgBox(reply, title="Confirmation of phrase")

>>> age = ui.InputBox('How old are you?', title="Hi")

>>> ui.Print(age)

Installering:

screen_io Pythonmodul


        # -*- coding: utf-8 -*-
        from __future__ import unicode_literals
        
        def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
            """ Viser eit dialogvindauge med ei melding og returnerer ein verdi.
            xScript = _getScript("_MsgBox")
            res = xScript.invoke((prompt,buttons,title), (), ())
            return res[0]
        
        def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:
            "" Viser ei melding i eit dialogvindauge der brukaren kan skriva inn tekst."" "
            xScript = _getScript("_InputBox")
            res = xScript.invoke((prompt,title,defaultValue), (), ())
            return res[0]
        
        def Print(message: str):
            """Skriv ut dei valde strengane eller numeriske uttrykka il eit dialogvindauge."""
            xScript = _getScript("_Print")
            xScript.invoke((message,), (), ())
        
        import uno
        from com.sun.star.script.provider import XScript
        def _getScript(script: str, library='Standard', module='uiScripts') -> XScript:
            sm = uno.getComponentContext().ServiceManager
            mspf = sm.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", uno.getComponentContext())
            scriptPro = mspf.createScriptProvider("")
            scriptName = "vnd.sun.star.script:"+library+"."+module+"."+script+"?language=Basic&location=application"
            xScript = scriptPro.getScript(scriptName)
            return xScript
    
note

Metodane MsgBox og InputBox frå Tenesta Basic inkludert i ScriptForge-biblioteka kallar opp dei opphavlege Basic-motpartane direkte.


uiSkript Basicmodul


        Option Explicit
        Private Function _MsgBox( prompt As String, Optional buttons As Integer, _
                Optional title As String ) As Integer
            _MsgBox = MsgBox( prompt, buttons, title )
        End Function
        Private Function _InputBox( prompt As String, Optional title As String, _
                Optional default As String) As String
            _InputBox = InputBox( prompt, title, default )
        End Function
        Private Sub _Print( msg As String )
            Print msg
        End Sub
    
tip

Utvidinga Alternative Python Script Organizer (APSO) har tilbod om ein msgbox()-funksjon i apso_utils-modulen sin.


Støtt oss!