Create a modeless dialog in Python UNO

The examples all seem to be in Basic.

I tried this (from here)

from com.sun.star.document.XEmbeddedScripts import DialogLibraries
my_lib = DialogLibraries.MyLibrary
dlg = CreateUnoDialog(my_lib.getByName("Dialog1"))
dlg.setVisible(True)

This firstly says “No module named ‘com’ (or ‘com.sun.star.document.XEmbeddedScripts.DialogLibraries’ is unknown) type <class ‘ImportError’>”
But in addition I don’t know where CreateUnoDialog comes from. It seems to be built-in when using Basic UNO. How do I use it when using Python?

import uno, unohelper
from com.sun.star.awt import XContainerWindowEventHandler, XTopWindowListener
g_URL = "vnd.sun.star.script:Standard.Dialog1?location=application"

class MyTopWindowListener(XTopWindowListener, unohelper.Base):
    def windowClosing(self, ev):
        ev.Source.dispose()

class CWEHandler(XContainerWindowEventHandler, unohelper.Base):
    def __init__(self):
        print('__init__')
    def callHandlerMethod(self, XWin, ev, sName):
        print(sName)
    def getSupportedMethodNames(self):
        return ('windowClosing',)

def Test_Dialog1_modeless(**args):
    smgr = uno.getComponentContext().getServiceManager()
    StarDesktop = smgr.createInstance('com.sun.star.frame.Desktop')
    oParent = StarDesktop.CurrentFrame.getContainerWindow()
    CWP = smgr.createInstance('com.sun.star.awt.ContainerWindowProvider')
    dlg = CWP.createContainerWindow(g_URL, "", oParent, CWEHandler())
    dlg.addTopWindowListener(MyTopWindowListener())
    dlg.setPosSize(200,200,500,500,15)
    dlg.setVisible(True)

def Test_Dialog1_modal(**args):
    smgr = uno.getComponentContext().getServiceManager()
    dp = smgr.createInstance('com.sun.star.awt.DialogProvider')
    dlg = dp.createDialog(g_URL)
    dlg.setPosSize(100,100, 500, 400, 15)
    x = dlg.execute()

The listener is needed if you want to be able to close the dialog without closing its parent window.

Thanks. Unfortunately I’m getting “Message: <class ‘uno.com.sun.star.lang.IllegalArgumentException’>: DialogProviderImpl::getDialogModel: dialog not found!” on the line
dlg = CWP.createContainerWindow(g_URL, "", oParent, CWEHandler())

Does variable g_URL need to be changed in some way?

The URL should point to an existing dialog. In my case it was “Dialog1” in Basic’s global “Standard” library (location=application).

1 Like

Tools → Macros → Organise dialogs. Made a simple dialog. Simple enough. Thanks.