diff --git a/AllLangHelp_sbasic.mk b/AllLangHelp_sbasic.mk index b76014f326..cc08cf424d 100644 --- a/AllLangHelp_sbasic.mk +++ b/AllLangHelp_sbasic.mk @@ -370,6 +370,7 @@ $(eval $(call gb_AllLangHelp_add_helpfiles,sbasic,\ helpcontent2/source/text/sbasic/python/python_ide \ helpcontent2/source/text/sbasic/python/python_locations \ helpcontent2/source/text/sbasic/python/python_programming \ + helpcontent2/source/text/sbasic/python/python_screen \ helpcontent2/source/text/sbasic/python/python_shell \ )) diff --git a/source/text/sbasic/python/python_examples.xhp b/source/text/sbasic/python/python_examples.xhp index 86e2aac535..40c00f8e34 100644 --- a/source/text/sbasic/python/python_examples.xhp +++ b/source/text/sbasic/python/python_examples.xhp @@ -21,6 +21,7 @@ Python;shell Python;platform Python;session + Python;screen input/output

Python programming examples

@@ -32,8 +33,8 @@ - - --> + + diff --git a/source/text/sbasic/python/python_screen.xhp b/source/text/sbasic/python/python_screen.xhp new file mode 100644 index 0000000000..a3d8077ee7 --- /dev/null +++ b/source/text/sbasic/python/python_screen.xhp @@ -0,0 +1,104 @@ + + + + + + Python : Screen Input/Output + /text/sbasic/python/python_screen.xhp + + + + + Python;InputBox + Python;MsgBox + Python;Print + +

Input/Output to Screen

+ Python standard output file is not available when running Python macros from Tools – Macros - Run Macro... 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. + The Alternative Python Script Organizer (APSO) extension offers a msgbox() function out of its apso_utils module. + %PRODUCTNAME Basic proposes InputBox(), Msgbox() and Print() screen I/O functions. Python alternatives exist relying either on %PRODUCTNAME 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 syntax:

+ MsgBox(txt, buttons=0, title=None)
+ InputBox(txt, title=None, default=None)
+ Print(txt) +

Examples:

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

Installation:

+ + + Copy screen_io Python module in My macros within <UserProfile>/Scripts/python/pythonpath, + + + Copy uiScripts Basic module in My macros Standard Basic library, + + + Restart %PRODUCTNAME. + + +

screen_io Python module

+ + # -*- coding: utf-8 -*- + from __future__ import unicode_literals + + def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int: + """ Displays a dialogue box containing a message and returns a value.""" + xScript = _getScript("_MsgBox") + res = xScript.invoke((prompt,buttons,title), (), ()) + return res[0] + + def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str: + """ Displays a prompt in a dialogue box at which the user can enter text.""" + xScript = _getScript("_InputBox") + res = xScript.invoke((prompt,title,defaultValue), (), ()) + return res[0] + + def Print(message: str): + """Outputs the specified strings or numeric expressions in a dialogue box.""" + 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 + +
+ + Calling Basic macros from Python + + + +
+ +
diff --git a/source/text/sbasic/shared/03010000.xhp b/source/text/sbasic/shared/03010000.xhp index bd9fae4df7..60039275dd 100644 --- a/source/text/sbasic/shared/03010000.xhp +++ b/source/text/sbasic/shared/03010000.xhp @@ -30,10 +30,8 @@ - -
- Screen I/O Functions +

Screen I/O Functions

This section describes the Runtime Functions used to call dialogs for the input and output of user entries.