Contingut
Índex 🔎︎

# Obre un diàleg amb Python

LibreOffice static dialogs are created with the Dialog editor and are stored in varying places according to their personal (My Macros), shared (Application Macros) or document-embedded nature. In reverse, dynamic dialogs are constructed at runtime, from Basic or Python scripts, or using any other LibreOffice supported language for that matter. Opening static dialogs with Python is illustrated herewith. Exception handling and internationalization are omitted for clarity.

## Diàlegs Les meves macros o Macros de l'aplicació

The examples below open Access2Base Trace console or the imported TutorialsDialog dialog with Tools - Macros - Run Macro menu:

            # -*- coding: utf-8 -*-
            from __future__ import unicode_literals

            def consoleDlg():
                ctx =XSCRIPTCONTEXT.getComponentContext()
                smgr = ctx.getServiceManager()
                dp = smgr.createInstanceWithContext("com.sun.star.awt.DialogProvider", ctx)
                dlg = dp.createDialog( "vnd.sun.star.script:Access2Base.dlgTrace?location=application")
                dlg.execute()
                dlg.dispose()

            def tutorDialog():
                ctx =XSCRIPTCONTEXT.getComponentContext()
                smgr = ctx.getServiceManager()
                dp = smgr.createInstanceWithContext("com.sun.star.awt.DialogProvider", ctx)
                dlg = dp.createDialog("vnd.sun.star.script:Standard.TutorialsDialog?location=application")
                dlg.execute()
                dlg.dispose()

            g_exportedScripts = (consoleDlg, tutorDialog)

## Diàlegs incrustats al document

The example below opens a newly edited Dialog1 dialog from a document with Tools - Macros - Run Macro menu:

            # -*- coding: utf-8 -*-
            from __future__ import unicode_literals

            def docDialog():
                """Mostra un diàleg basat en un document"""
                model = XSCRIPTCONTEXT.getDocument()
                smgr = XSCRIPTCONTEXT.getComponentContext().ServiceManager
                dp = smgr.createInstanceWithArguments( "com.sun.star.awt.DialogProvider", (model,))
                dlg = dp.createDialog( "vnd.sun.star.script:Standard.Dialog1?location=document")
                dlg.execute()
                dlg.dispose()

            g_exportedScripts = (docDialog,)

Consulteu msgbox.py al directori {installation}/programa/ per a exemples de diàlegs dinàmics en Python.

Temes relacionats

Obertura d'un diàleg amb el Basic

Exemples de programació en Python

Ajuda dels scripts de Python en el LibreOffice