Invoer/Uitvoer naar Scherm

Python standaard uitvoerbestand is niet beschikbaar wanneer er Python-macro's worden uitgevoerd vanuit Extra – Macro's - Macro uitvoeren... menu. Voor het weergeven van de uitvoer van een module is de Pyton interactieve console vereist. Functies zoals input(), print(), repr() en str() zijn beschikbaar vanaf de Python shell.

Tippictogram

De Alternatieve Python Script Organizer(APSO) extensie, verzorgt een msgbox() functie vanuit zijn apso_utils module.


LibreOffice Basic proposes InputBox(), Msgbox() and Print() screen I/O functions. Python alternatives exist relying either on LibreOffice API Abstract Windowing Toolkit, either on Python to Basic function calls. The latter proposes a syntax that is intentionally close to that of Basic, and uses a Python module next to a Basic module. The API Scripting Framework is used to perform Basic, BeanShell, JavaScript and Python inter-languages function calls.

Python syntaxis:

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

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

Print(txt)

Voorbeelden:

>>> import screen_io as ui

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

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

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

>>> ui.Print(age)

Installatie:

screen_io Python module


        # -*- coding: utf-8 -*-
        from __future__ import unicode_literals
        
        def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
            """ Geef een dialoogvenster weer die een bericht bevat en een waarde teruggeeft."""
            xScript = _getScript("_MsgBox")
            res = xScript.invoke((prompt,buttons,title), (), ())
            return res[0]
        
        def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:
            """ Geeft een prompt weer in een dialoogvenster waarin de gebruiker tekst kan invoeren."""
            xScript = _getScript("_InputBox")
            res = xScript.invoke((prompt,title,defaultValue), (), ())
            return res[0]
        
        def Print(message: str):
            '''Schrijft de gespecificeerde tekenreeksen of numerieke uitdrukkingen weg naar een dialoogvenster.'''
            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
    

uiScripts Basic-module


        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