Είσοδος/Έξοδος στην οθόνη

Το τυπικό αρχείο εξόδου Python δεν είναι διαθέσιμο κατά την εκτέλεση μακροεντολών Python από το μενού Εργαλεία – Μακροεντολές – Εκτέλεση μακροεντολής... .Η εμφάνιση της εξόδου ενός αρθρώματος απαιτεί διαδραστική κονσόλα Python. Χαρακτηριστικά όπως input(), print(), repr() και str() είναι διαθέσιμα από το κέλυφος Python.

tip

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


Η Basic του LibreOffice προτείνει συναρτήσεις εισόδου/εξόδου οθόνης InputBox(), Msgbox() και Print(). Υπάρχουν εναλλακτικές του Python που βασίζονται είτε στο API του LibreOffice Abstract Windowing Toolkit, είτε σε κλήσεις συναρτήσεων Python σε Basic. Η δεύτερη προτείνει σύνταξη που εκ προθέσεως είναι κοντά στη Basic και χρησιμοποιεί άρθρωμα Python δίπλα σε άρθρωμα Basic. Η API πλαισίου δέσμης ενεργειών χρησιμοποιείται για την εκτέλεση διαγλωσσικών κλήσεων συναρτήσεων BeanShell, JavaScript και Python.

Σύνταξη Python:

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

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

Print(txt)

Παραδείγματα:

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

Εγκατάσταση:

Άρθρωμα Python screen_io


        # -*- coding: utf-8 -*-
        from __future__ import unicode_literals
        
        def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
            """ Εμφανίζει πλαίσιο διαλόγου που περιέχει μήνυμα και επιστρέφει τιμή. """
            xScript = _getScript("_MsgBox")
            res = xScript.invoke((prompt,buttons,title), (), ())
            return res[0]
        
        def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:
            """ Εμφανίζει προτροπή σε πλαίσιο διαλόγου στο οποίο ο χρήστης μπορεί να εισάγει κείμενο."""
            xScript = _getScript("_InputBox")
            res = xScript.invoke((prompt,title,defaultValue), (), ())
            return res[0]
        
        def Print(message: str):
            """Εξάγει τις συγκεκριμένες συμβολοσειρές ή αριθμητικές εκφράσεις σε πλαίσιο διαλόγου."""
            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 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.