Ein-/Ausgabe auf dem Bildschirm

Die Python-Standardausgabedatei ist nicht verfügbar, wenn Python-Makros über Extras – Makros – Makro ausführen… ausgeführt werden. Für die Darstellung der Ausgabe eines Moduls ist die interaktive Python-Konsole erforderlich. Funktionen wie input(), print(), repr() und str() sind über die Python-Shell verfügbar.

tip

LibreOffice msgbox Python module proposes a msgbox() method that is illustrated in Creating Event Listeners and Creating a dialog handler example pages.


LibreOffice Basic bietet InputBox(), Msgbox() und Print() Bildschirm-Ein-/Ausgabefunktionen. Es gibt Python-Alternativen, die entweder auf der LibreOffice API "Abstract Windowing Toolkit" oder auf Funktionsaufrufen von Python zu Basic basieren. Letzteres schlägt eine Syntax vor, die absichtlich der von Basic nahe kommt, und verwendet ein Python-Modul neben einem Basic-Modul. Die API "Scripting Framework" wird verwendet, um Funktionsaufrufe zwischen den Sprachen Basic, BeanShell, JavaScript und Python auszuführen.

Python-Syntax:

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

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

Print(txt)

Beispiele:

>>> 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)

Installation:

Python-Modul screen_io


        # -*- coding: utf-8 -*-
        from __future__ import unicode_literals
        
        def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
            """ Zeigt einen Dialog mit einer Nachricht an und gibt einen Wert zurück. """
            xScript = _getScript("_MsgBox")
            res = xScript.invoke((prompt,buttons,title), (), ())
            return res[0]
        
        def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:
            """ Zeigt eine Eingabeaufforderung in einem Dialog an, in dem der Benutzer Text eingeben kann. """
            xScript = _getScript("_InputBox")
            res = xScript.invoke((prompt,title,defaultValue), (), ())
            return res[0]
        
        def Print(message: str):
            """ Gibt die angegebenen Zeichenfolgen oder numerischen Ausdrücke in einem Dialog aus. """
            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

MsgBox and InputBox methods from the Basic service included in the ScriptForge libraries call directly their native Basic counterparts.


Basic-Modul uiScripts


        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

The Alternative Python Script Organizer (APSO) extension offers a msgbox() function out of its apso_utils module.