blob: 7f728b495058ef3bbbb0cfadd42aed4874724938 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<?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_Dialog">
<title id="tit" xml-lang="en-US">Opening a Dialog with Python</title>
<filename>/text/sbasic/python/python_dialogs.xhp</filename>
</topic>
</meta>
<body>
<bookmark branch="index" id="N0334">
<bookmark_value>Python;dialogs</bookmark_value>
<bookmark_value>dialog box;Python</bookmark_value>
<bookmark_value>dialogs;Python</bookmark_value>
</bookmark>
<section id="pythondialog1">
<h1 id="N0336"><variable id="pythondialog"><link href="text/sbasic/python/python_dialogs.xhp" name="command_name">Opening a Dialog with Python</link></variable></h1>
</section>
<paragraph role="paragraph" id="N0337">%PRODUCTNAME static dialogs are created with the <link href="text/sbasic/guide/create_dialog.xhp" name="dialog editor">Dialog editor</link> and are stored in varying places according to their personal (My Macros), shared (%PRODUCTNAME Macros) or document-embedded nature. In reverse, dynamic dialogs are constructed at runtime, from Basic or Python scripts, or using any other <link href="text/shared/guide/scripting.xhp">%PRODUCTNAME supported language</link> for that matter. Opening static dialogs with Python is illustrated herewith. Exception handling and internationalization are omitted for clarity.</paragraph>
<h2 id="N0338">My Macros or %PRODUCTNAME Macros dialogs</h2>
<paragraph role="paragraph" id="N0339">The examples below open <literal>Access2Base Trace</literal> console or the imported <literal>TutorialsDialog</literal> dialog with <menuitem>Tools – Macros – Run Macro...</menuitem> menu:</paragraph>
<pycode>
<paragraph role="pycode" id="N0340" localize="false"># -*- coding: utf-8 -*-</paragraph>
<paragraph role="pycode" id="N0341" localize="false">from __future__ import unicode_literals</paragraph>
<paragraph role="pycode" id="N0342" localize="false"> </paragraph>
<paragraph role="pycode" id="N0343" localize="false">def consoleDlg():</paragraph>
<paragraph role="pycode" id="N0344" localize="false"> ctx =XSCRIPTCONTEXT.getComponentContext()</paragraph>
<paragraph role="pycode" id="N0345" localize="false"> smgr = ctx.getServiceManager()</paragraph>
<paragraph role="pycode" id="N0346" localize="false"> dp = smgr.createInstanceWithContext("com.sun.star.awt.DialogProvider", ctx)</paragraph>
<paragraph role="pycode" id="N0348" localize="false"> dlg = dp.createDialog( "vnd.sun.star.script:Access2Base.dlgTrace?location=application")</paragraph>
<paragraph role="pycode" id="N0350" localize="false"> dlg.execute()</paragraph>
<paragraph role="pycode" id="N0351" localize="false"> dlg.dispose()</paragraph>
<paragraph role="pycode" id="N0352" localize="false"> </paragraph>
<paragraph role="pycode" id="N0353" localize="false">def tutorDialog():</paragraph>
<paragraph role="pycode" id="N0354" localize="false"> ctx =XSCRIPTCONTEXT.getComponentContext()</paragraph>
<paragraph role="pycode" id="N0355" localize="false"> smgr = ctx.getServiceManager()</paragraph>
<paragraph role="pycode" id="N0356" localize="false"> dp = smgr.createInstanceWithContext("com.sun.star.awt.DialogProvider", ctx)</paragraph>
<paragraph role="pycode" id="N0358" localize="false"> dlg = dp.createDialog("vnd.sun.star.script:Standard.TutorialsDialog?location=application")</paragraph>
<paragraph role="pycode" id="N0360" localize="false"> dlg.execute()</paragraph>
<paragraph role="pycode" id="N0361" localize="false"> dlg.dispose()</paragraph>
<paragraph role="pycode" id="N0362" localize="false"> </paragraph>
<paragraph role="pycode" id="N0363" localize="false">g_exportedScripts = (consoleDlg, tutorDialog)</paragraph>
</pycode>
<h2 id="N0364">Document embedded dialogs</h2>
<paragraph role="paragraph" id="N0365">The example below opens a newly edited <literal>Dialog1</literal> dialog from a document with <menuitem>Tools – Macros – Run Macro...</menuitem> menu:</paragraph>
<pycode>
<paragraph role="pycode" id="N0366" localize="false"># -*- coding: utf-8 -*-</paragraph>
<paragraph role="pycode" id="N0367" localize="false">from __future__ import unicode_literals</paragraph>
<paragraph role="pycode" id="N0368" localize="false"> </paragraph>
<paragraph role="pycode" id="N0369" localize="false">def docDialog():</paragraph>
<paragraph role="pycode" id="N0370"> """ Display a doc-based dialog """</paragraph>
<paragraph role="pycode" id="N0371" localize="false"> model = XSCRIPTCONTEXT.getDocument()</paragraph>
<paragraph role="pycode" id="N0372" localize="false"> smgr = XSCRIPTCONTEXT.getComponentContext().ServiceManager</paragraph>
<paragraph role="pycode" id="N0373" localize="false"> dp = smgr.createInstanceWithArguments( "com.sun.star.awt.DialogProvider", (model,))</paragraph>
<paragraph role="pycode" id="N0375" localize="false"> dlg = dp.createDialog( "vnd.sun.star.script:Standard.Dialog1?location=document")</paragraph>
<paragraph role="pycode" id="N0377" localize="false"> dlg.execute()</paragraph>
<paragraph role="pycode" id="N0378" localize="false"> dlg.dispose()</paragraph>
<paragraph role="pycode" id="N0379" localize="false"> </paragraph>
<paragraph role="pycode" id="N0380" localize="false">g_exportedScripts = (docDialog,)</paragraph>
</pycode>
<paragraph role="paragraph" id="N0381">Refer to <literal>msgbox.py</literal> in <literal>{installation}/program/</literal> directory for Python dynamic dialog examples.</paragraph>
<section id="relatedtopics" >
<embed href="text/sbasic/guide/show_dialog.xhp#show_dialog"/>
<embed href="text/sbasic/python/python_examples.xhp#pythonexamples2"/>
<embed href="text/sbasic/python/main0000.xhp#pythonscriptshelp"/>
</section>
</body>
</helpdocument>
|