Macros/General/IO to Screen

Python standard console output is not available when running Python macros from Tools – Macros – Run Macro… LibreOffice menu. Presenting the output of a module requires the Python interactive console. Features such as input, print, repr and str are available from the Python shell.

LibreOffice Basic proposes InputBox, Msgbox and Print screen I/O functions. Python alternatives exist that rely either on LibreOffice API Abstract Windowing Toolkit, either on msgbox.py LibreOffice shared module, either on Python to Basic function calls. Proposed method signatures in the coming examples are intentionally close to that of Basic. The API Scripting Framework can be used to perform Basic, BeanShell, JavaScript and Python inter-languages function calls.

Screen InputBox function
Cloning InputBox Basic function can be achieved using LibreOffice API calls or Python to Basic inter-languages calls.

Using Application Programming Interface (API)
There is no InputBox function provided for Python. The following example code is borrowed from Transfer from Basic to Python. The script builds the complete dialog plus its controls using the API abstract windowing toolkit. An instance of ''' com.sun.star. awt.UnoControlDialogModel service gets created, and various  com.sun.star. awt.UnoControlxxxModel''' controls are added. ''' com.sun.star. awt.Toolkit service displays the dialog as  com.sun.star. frame.Desktop''' environment locates the current window. Example:

Using X-Scripting
That mechanism is described in Input/Output to Screen help page. screen_io.InputBox function signature is identical to that of LibreOffice Basic InputBox function.

MsgBox function
MsgBox function alternatives exist using either LibreOffice API, either msgbox.py module or Python to Basic calls. Available features differ depending on the solution. They are summarized herewith:

Using the API
The message dialog is performed by calling the createMessageBox method of the ''' com.sun.star. awt.Toolkit''' service. A very similar example is present in Python Guide - Useful functions page. The Developer's Guide illustrates that same approach using Java.

Examples:

Using msgbox.py
This module comes bundled with LibreOffice. Use it to build custom message dialogs that require user-defined buttons. Return value is that of the selected button. Dialog closing is inhibited. Its MsgBox class method signatures are:

Examples:
 * MsgBox(ctx: “Component context”)
 * addButton(caption: str)
 * renderFromBoxSize(size: num = 150)
 * renderFromButtonsSize(size: int = 50)
 * show(message: str, decoration: int, title: str): str

Using X-Scripting
That mechanism is described in Input/Output to Screen help page. screen_io.MsgBox function signature is identical to that of LibreOffice Basic MsgBox function or statement.

Print display function
Cloning Print function can be achieved with Python to Basic inter-languages calls or using msgbox.py Python module.

Using msgbox.py
This module comes bundled with LibreOffice. Use it to build custom message dialogs that require user-defined buttons. Return value is that of the selected button. Dialog closing is inhibited. Its MsgBox class method signatures are:

Examples:
 * MsgBox(ctx: “Component context”)
 * addButton(caption: str)
 * renderFromBoxSize(size: num = 150)
 * renderFromButtonsSize(size: int = 50)
 * show(message: str, decoration: int, title: str): str

Using X-Scripting
That mechanism is described in Input/Output to Screen help page. screen_io.Print function signature is intentionally close to that of LibreOffice Basic Print statement.