diff options
Diffstat (limited to 'helpcontent2/source/text/sbasic/python/python_screen.xhp')
-rw-r--r-- | helpcontent2/source/text/sbasic/python/python_screen.xhp | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/helpcontent2/source/text/sbasic/python/python_screen.xhp b/helpcontent2/source/text/sbasic/python/python_screen.xhp new file mode 100644 index 0000000000..227bc0290d --- /dev/null +++ b/helpcontent2/source/text/sbasic/python/python_screen.xhp @@ -0,0 +1,107 @@ +<?xml version="1.0" encoding="UTF-8"?> +<helpdocument version="1.0"> + <!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + --> + <meta> + <topic id="text/sbasic/python/python_screen" indexer="include" status="PUBLISH"> + <title id="tit">Python : Screen Input/Output</title> + <filename>/text/sbasic/python/python_screen.xhp</filename> + </topic> + </meta> + <body> + <bookmark branch="index" xml-lang="en-US" id="N0433"> + <bookmark_value>Python;InputBox</bookmark_value> + <bookmark_value>Python;MsgBox</bookmark_value> + <bookmark_value>Python;Print</bookmark_value> + <bookmark_value>API;script.provider.MasterScriptProvider: Screen Input/Output</bookmark_value> + <bookmark_value>API;script.provider.XScript: Screen Input/Output</bookmark_value> + </bookmark> + <h1 id="N0434"><variable id="ioscreen"><link href="text/sbasic/python/python_screen.xhp">Input/Output to Screen</link></variable></h1> + <paragraph role="paragraph" id="N0435">Python standard output file is not available when running Python macros from <menuitem>Tools – Macros - Run Macro</menuitem>... menu. Presenting the output of a module requires the Python interactive console. Features such as <literal>input()</literal>, <literal>print()</literal>, <literal>repr()</literal> and <literal>str()</literal> are available from the Python shell.</paragraph> + <tip id="msgbox_tip">%PRODUCTNAME <literal>msgbox</literal> Python module proposes a <literal>msgbox()</literal> method that is illustrated in <link href="text/sbasic/python/python_handler.xhp">Creating Event Listeners</link> and <link href="text/sbasic/python/python_handler.xhp">Creating a dialog handler</link> example pages.</tip> + <paragraph role="paragraph" id="N0437">%PRODUCTNAME Basic proposes <literal>InputBox()</literal>, <literal>Msgbox()</literal> and <literal>Print()</literal> 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.</paragraph> + <h2 id="N0438">Python syntax:</h2> + <paragraph role="code" id="N0439" localize="false">MsgBox(txt, buttons=0, title=None)<br/></paragraph> + <paragraph role="code" id="N0440" localize="false">InputBox(txt, title=None, default=None)<br/></paragraph> + <paragraph role="code" id="N0441" localize="false">Print(txt)</paragraph> + <h2 id="N0442">Examples:</h2> + <paragraph role="paragraph" localize="false" id="N0443"><literal>>>> import screen_io as ui</literal></paragraph> + <paragraph role="paragraph" localize="false" id="N0445"><literal>>>> reply = ui.InputBox('Please enter a phrase', title='Dear user', defaultValue="here..")</literal></paragraph> + <paragraph role="paragraph" localize="false" id="N0446"><literal>>>> rc = ui.MsgBox(reply, title="Confirmation of phrase")</literal></paragraph> + <paragraph role="paragraph" localize="false" id="N0447"><literal>>>> age = ui.InputBox('How old are you?', title="Hi")</literal></paragraph> + <paragraph role="paragraph" localize="false" id="N0448"><literal>>>> ui.Print(age)</literal></paragraph> + <h2 id="N0449">Installation:</h2> + <list type="unordered"> + <listitem> + <paragraph role="listitem" id="N0450">Copy <literal>screen_io</literal> Python module in <link href="text/sbasic/python/python_locations.xhp">My macros</link> within <UserProfile>/Scripts/python/pythonpath,</paragraph> + </listitem> + <listitem> + <paragraph role="listitem" id="N0451">Copy <literal>uiScripts</literal> Basic module in <link href="text/sbasic/python/python_locations.xhp">My macros</link> Standard Basic library,</paragraph> + </listitem> + <listitem> + <paragraph role="listitem" id="N0452">Restart %PRODUCTNAME.</paragraph> + </listitem> + </list> + <h3 id="N0453"><literal>screen_io</literal> Python module</h3> + <pycode> + <paragraph role="pycode" localize="false" id="N0454"># -*- coding: utf-8 -*-</paragraph> + <paragraph role="pycode" localize="false" id="N0455">from __future__ import unicode_literals</paragraph> + <paragraph role="pycode" localize="false" id="N0456"></paragraph> + <paragraph role="pycode" localize="false" id="N0466">def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:</paragraph> + <paragraph role="pycode" id="N0467"> """ Displays a dialog box containing a message and returns a value."""</paragraph> + <paragraph role="pycode" localize="false" id="N0468"> xScript = _getScript("_MsgBox")</paragraph> + <paragraph role="pycode" localize="false" id="N0469"> res = xScript.invoke((prompt,buttons,title), (), ())</paragraph> + <paragraph role="pycode" localize="false" id="N0470"> return res[0]</paragraph> + <paragraph role="pycode" localize="false" id="N0471"></paragraph> + <paragraph role="pycode" localize="false" id="N0472">def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:</paragraph> + <paragraph role="pycode" id="N0473"> """ Displays a prompt in a dialog box at which the user can enter text."""</paragraph> + <paragraph role="pycode" localize="false" id="N0474"> xScript = _getScript("_InputBox")</paragraph> + <paragraph role="pycode" localize="false" id="N0475"> res = xScript.invoke((prompt,title,defaultValue), (), ())</paragraph> + <paragraph role="pycode" localize="false" id="N0476"> return res[0]</paragraph> + <paragraph role="pycode" localize="false" id="N0477"></paragraph> + <paragraph role="pycode" localize="false" id="N0478">def Print(message: str):</paragraph> + <paragraph role="pycode" id="N0479"> """Outputs the specified strings or numeric expressions in a dialog box."""</paragraph> + <paragraph role="pycode" localize="false" id="N0480"> xScript = _getScript("_Print")</paragraph> + <paragraph role="pycode" localize="false" id="N0481"> xScript.invoke((message,), (), ())</paragraph> + <paragraph role="pycode" localize="false" id="N0482"></paragraph> + <paragraph role="pycode" localize="false" id="N0483">import uno</paragraph> + <paragraph role="pycode" localize="false" id="N0484">from com.sun.star.script.provider import XScript</paragraph> + <paragraph role="pycode" localize="false" id="N0485">def _getScript(script: str, library='Standard', module='uiScripts') -> XScript:</paragraph> + <paragraph role="pycode" localize="false" id="N0486"> sm = uno.getComponentContext().ServiceManager</paragraph> + <paragraph role="pycode" localize="false" id="N0487"> mspf = sm.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", uno.getComponentContext())</paragraph> + <paragraph role="pycode" localize="false" id="N0488"> scriptPro = mspf.createScriptProvider("")</paragraph> + <paragraph role="pycode" localize="false" id="N0489"> scriptName = "vnd.sun.star.script:"+library+"."+module+"."+script+"?language=Basic&location=application"</paragraph> + <paragraph role="pycode" localize="false" id="N0490"> xScript = scriptPro.getScript(scriptName)</paragraph> + <paragraph role="pycode" localize="false" id="N0491"> return xScript</paragraph> + </pycode> + <note id="par_id161655364816553"><literal>MsgBox</literal> and <literal>InputBox</literal> methods from the <link href="text/sbasic/shared/03/sf_basic.xhp">Basic service</link> included in <link href="text/sbasic/shared/03/lib_ScriptForge.xhp"> the ScriptForge libraries</link> call directly their native Basic counterparts.</note> + <h3 id="N0492"><literal>uiScripts</literal> Basic module</h3> + <bascode> + <paragraph role="bascode" localize="false" id="N0493">Option Explicit</paragraph> + <paragraph role="bascode" localize="false" id="N0494">Private Function _MsgBox( prompt As String, Optional buttons As Integer, _</paragraph> + <paragraph role="bascode" localize="false" id="N0495"> Optional title As String ) As Integer</paragraph> + <paragraph role="bascode" localize="false" id="N0496"> _MsgBox = MsgBox( prompt, buttons, title )</paragraph> + <paragraph role="bascode" localize="false" id="N0497">End Function</paragraph> + <paragraph role="bascode" localize="false" id="N0498">Private Function _InputBox( prompt As String, Optional title As String, _</paragraph> + <paragraph role="bascode" localize="false" id="N0499"> Optional default As String) As String</paragraph> + <paragraph role="bascode" localize="false" id="N0500"> _InputBox = InputBox( prompt, title, default )</paragraph> + <paragraph role="bascode" localize="false" id="N0501">End Function</paragraph> + <paragraph role="bascode" localize="false" id="N0502">Private Sub _Print( msg As String )</paragraph> + <paragraph role="bascode" localize="false" id="N0503"> Print msg</paragraph> + <paragraph role="bascode" localize="false" id="N0504">End Sub</paragraph> + </bascode> + <tip id="tip_APSO">The <link href="https://extensions.libreoffice.org/extensions/apso-alternative-script-organizer-for-python">Alternative Python Script Organizer</link> (APSO) extension offers a msgbox() function out of its <literal>apso_utils</literal> module.</tip> + <section id="relatedtopics"> + <embed href="text/sbasic/shared/03010000.xhp#BasicScreenIO"/> + <embed href="text/sbasic/python/python_examples.xhp#pythonexamples2"/> + <embed href="text/sbasic/python/main0000.xhp#pythonscriptshelp"/> + <embed href="text/sbasic/python/python_2_basic.xhp#py2ba_h1"/> + </section> +</body> +</helpdocument> |