Page 1 of 1

[Solved] Dialog box in Python

PostPosted: Thu Sep 20, 2012 5:31 pm
by malmeida
Anyone knows how to create a dialog box with two text inputs in one Python Macro? (similiar to the attached example).

Another away that i think it works, is to create the dialog box in the OOo interface, attach them to the file and call him from the python script macro. Is this possible?

Screenshot.png (5.21 KiB) Viewed 9715 times


Re: Dialog box in Python

PostPosted: Thu Sep 20, 2012 8:34 pm
by Villeroy
1 ... Model.html
2 ... Model.html
2 ... Model.html
1 ... Model.html
Set all model properties, attach the 5 control models to the dialog model, load the dialog, get the loaded dialog and its form controls, resize and arrange them execute the dialog.

Re: Dialog box in Python

PostPosted: Thu Sep 20, 2012 8:38 pm
You can also call a dialog that is stored in MyMacros like this
Code: Select all   Expand viewCollapse view
def littleDialog():
  Doc = XSCRIPTCONTEXT.getDocument() 
  psm = uno.getComponentContext().ServiceManager
  dp = psm.createInstance("")
  dlg = dp.createDialog("") 

Re: Dialog box in Python

PostPosted: Fri Sep 21, 2012 5:53 pm
by malmeida
I found it one way do build the desired interface directly in python.

Here it is an example:

Code: Select all   Expand viewCollapse view
import uno
import unohelper
from import XActionListener

class MyActionListener( unohelper.Base, XActionListener ):
    def __init__(self, labelControl, prefix ):
        self.nCount = 0
        self.labelControl = labelControl
        self.prefix = prefix
    def actionPerformed(self, actionEvent):
        # increase click counter
        self.nCount = self.nCount + 1;
        self.labelControl.setText( self.prefix + str( self.nCount ) )

# 'translated' from the developer's guide chapter 11.6
def createDialog():
    """Opens a dialog with a push button and a label, clicking the button increases the label counter."""
        ctx = uno.getComponentContext()
        smgr = ctx.ServiceManager

        # create the dialog model and set the properties
        dialogModel = smgr.createInstanceWithContext(
            "", ctx)

        dialogModel.PositionX = 100
        dialogModel.PositionY = 100
        dialogModel.Width = 150
        dialogModel.Height = 100
        dialogModel.Title = "Runtime Dialog Demo"

        # create the button model and set the properties
        buttonModel = dialogModel.createInstance(
            "" )

        buttonModel.PositionX = 50
        buttonModel.PositionY  = 30
        buttonModel.Width = 50;
        buttonModel.Height = 14;
        buttonModel.Name = "myButtonName";
        buttonModel.TabIndex = 0;         
        buttonModel.Label = "Click Me";

        # create the label model and set the properties
        labelModel = dialogModel.createInstance(
            "" );

        labelModel.PositionX = 40
        labelModel.PositionY = 60
        labelModel.Width  = 100
        labelModel.Height = 14
        labelModel.Name = "myLabelName"
        labelModel.TabIndex = 1
        labelModel.Label = "Clicks "

        # insert the control models into the dialog model
        dialogModel.insertByName( "myButtonName", buttonModel);
        dialogModel.insertByName( "myLabelName", labelModel);

        # create the dialog control and set the model
        controlContainer = smgr.createInstanceWithContext(
            "", ctx);

        # add the action listener
            MyActionListener( controlContainer.getControl( "myLabelName" ), labelModel.Label ))

        # create a peer
        toolkit = smgr.createInstanceWithContext(
            "", ctx);       

        controlContainer.createPeer(toolkit, None);

        # execute it

        # dispose the dialog
    except Exception,e:
        print str(e)

g_exportedScripts = createDialog,

Re: Dialog box in Python

PostPosted: Fri Sep 21, 2012 6:16 pm
by hanya
I suggest people to use and its family to instantiate dialog from xdl file generated by Basic IDE dialog editor.
When the dialog instantiated by the dialog provider, dialog elements are wrapped by a wrapper class and it is delegated by the element and it also provides some properties such as PositionX, PositionY, Step and so on. Step properties are useful to make multistep dialog. If the dialog constructed by hand, these properties are not provided.
Even if someone want to add controls that can not be make using dialog editor of the Basic IDE, instantiate empty dialog from xdl file and create its elements with createInstance method of interface, its result is always wrapped by the wrapper mentioned above.

Here is a way to instantiate dialog from document embedded library using the dialog provider: ... highlight=

Re: Dialog box in Python

PostPosted: Fri Sep 21, 2012 6:36 pm
by malmeida
Thanks hanya, it was great. But how can i get the input variables from the dialog?

Re: Dialog box in Python

PostPosted: Fri Sep 21, 2012 7:05 pm
by hanya
You can access all elements on the dialog from dialog object returned by the dialog provider, use object inspector to find what kind of value can be get from each element and see module in IDL reference. You need to understand View-Model model also.

The following code written in Basic but almost the same:
Code: Select all   Expand viewCollapse view
Sub DoSomethingWithDialog
  sURI = ""
  oDialog = CreateUnoService("").createDialog(sURI)
  vResult = nothing
  ' control view
  oButton = oDialog.getControl("CommandButton1")
  If oDialog.execute() = 1 Then
    ' control model
    oTextField1Model = oDialog.getModel().getByName("TextField1")
    vResult = oTextField1Model.Text  ' current text data
  End If
End Sub

Re: [Solved] Dialog box in Python

PostPosted: Sat Sep 29, 2012 7:32 pm
by Villeroy
Another log-in dialog on the fly. It has a helper function to simply add control models to a dialog model using a dictionary as property container.
Then it creates a dialog from that model and arranges the controls in the most simple manner.
Code: Select all   Expand viewCollapse view
import uno
from import POSSIZE

def addAwtModel(oDM,srv,sName,dProps):
    '''Insert UnoControl<srv>Model into given DialogControlModel oDM by given sName and properties dProps'''
    oCM = oDM.createInstance(""+ srv +"Model")
    while dProps:
        prp = dProps.popitem()
        #works with awt.UnoControlDialogElement only:
        oCM.Name = sName

def getLogin():
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    oDM = smgr.createInstance("")
    oDM.Title = 'Login Dialog'
        'Label' : 'User Name',
        'Label' : 'Password',
        'EchoChar' : 42,
        'Label' : 'OK',
        'DefaultButton' : True,
        'PushButtonType' : 1,
        'Label' : 'Cancel',
        'PushButtonType' : 2,

    oDialog = smgr.createInstance("")
    txtN = oDialog.getControl('txtName')
    txtP = oDialog.getControl('txtPWD')
    h = 25
    y = 10
    for c in oDialog.getControls():
        c.setPosSize(10, y, 200, h, POSSIZE)
        y += h
    x = oDialog.execute()
    if x:
        return (txtN.getText(),txtP.getText())
        return False