[Solved] Dialog box in Python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
malmeida
Posts: 4
Joined: Thu Sep 20, 2012 5:17 pm

[Solved] Dialog box in Python

Post 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
Screenshot.png (5.21 KiB) Viewed 12097 times
Thanks
Last edited by malmeida on Fri Sep 21, 2012 7:16 pm, edited 1 time in total.
OpenOffice 3.0.1
Ubuntu 9.04
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Dialog box in Python

Post by Villeroy »

1 http://www.openoffice.org/api/docs/comm ... Model.html
2 http://www.openoffice.org/api/docs/comm ... Model.html
2 http://www.openoffice.org/api/docs/comm ... Model.html
1 http://www.openoffice.org/api/docs/comm ... 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Dialog box in Python

Post by FJCC »

You can also call a dialog that is stored in MyMacros like this

Code: Select all

#http://www.oooforum.org/forum/viewtopic.phtml?t=23586&highlight=call+python+basic
def littleDialog(): 
  Doc = XSCRIPTCONTEXT.getDocument()  
  psm = uno.getComponentContext().ServiceManager 
  dp = psm.createInstance("com.sun.star.awt.DialogProvider") 
  dlg = dp.createDialog("vnd.sun.star.script:Standard.Dialog1?location=application")  
  dlg.execute()
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
malmeida
Posts: 4
Joined: Thu Sep 20, 2012 5:17 pm

Re: Dialog box in Python

Post by malmeida »

I found it one way do build the desired interface directly in python.

Here it is an example:

Code: Select all

import uno
import unohelper
from com.sun.star.awt 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."""
    try:
        ctx = uno.getComponentContext()
        smgr = ctx.ServiceManager

        # create the dialog model and set the properties 
        dialogModel = smgr.createInstanceWithContext( 
            "com.sun.star.awt.UnoControlDialogModel", 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( 
            "com.sun.star.awt.UnoControlButtonModel" )

        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( 
            "com.sun.star.awt.UnoControlFixedTextModel" ); 

        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( 
            "com.sun.star.awt.UnoControlDialog", ctx); 
        controlContainer.setModel(dialogModel); 

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

        # create a peer 
        toolkit = smgr.createInstanceWithContext( 
            "com.sun.star.awt.ExtToolkit", ctx);       

        controlContainer.setVisible(False);       
        controlContainer.createPeer(toolkit, None);

        # execute it
        controlContainer.execute()

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

g_exportedScripts = createDialog,
OpenOffice 3.0.1
Ubuntu 9.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Dialog box in Python

Post by hanya »

I suggest people to use com.sun.star.awt.DialogProvider 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 com.sun.star.lang.XMultiServiceFactory 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: http://www.oooforum.org/forum/viewtopic ... highlight=
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
malmeida
Posts: 4
Joined: Thu Sep 20, 2012 5:17 pm

Re: Dialog box in Python

Post by malmeida »

Thanks hanya, it was great. But how can i get the input variables from the dialog?
OpenOffice 3.0.1
Ubuntu 9.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Dialog box in Python

Post 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 com.sun.star.awt 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

Sub DoSomethingWithDialog
  sURI = "vnd.sun.star.script:Standard.Dialog1?location=application"
  oDialog = CreateUnoService("com.sun.star.awt.DialogProvider").createDialog(sURI)
  vResult = nothing
  ' control view
  oButton = oDialog.getControl("CommandButton1")
  'oButton.addActionListener(.....)
  If oDialog.execute() = 1 Then
    ' control model
    oTextField1Model = oDialog.getModel().getByName("TextField1")
    vResult = oTextField1Model.Text  ' current text data
  End If
  oDialog.dispose()
End Sub
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Dialog box in Python

Post 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

import uno
from com.sun.star.awt.PosSize 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("com.sun.star.awt.UnoControl"+ srv +"Model")
    while dProps:
        prp = dProps.popitem()
        uno.invoke(oCM,"setPropertyValue",(prp[0],prp[1]))
        #works with awt.UnoControlDialogElement only:
        oCM.Name = sName
    oDM.insertByName(sName,oCM)

def getLogin():
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    oDM = smgr.createInstance("com.sun.star.awt.UnoControlDialogModel")
    oDM.Title = 'Login Dialog'
    addAwtModel(oDM,'FixedText','lblName',{
        'Label' : 'User Name',
        }
                )
    addAwtModel(oDM,'Edit','txtName',{})
    addAwtModel(oDM,'FixedText','lblPWD',{
        'Label' : 'Password',
        }
                )
    addAwtModel(oDM,'Edit','txtPWD',{
        'EchoChar' : 42,
        }
                )
    addAwtModel(oDM,'Button','btnOK',{
        'Label' : 'OK',
        'DefaultButton' : True,
        'PushButtonType' : 1,
        }
                )
    addAwtModel(oDM,'Button','btnCancel',{
        'Label' : 'Cancel',
        'PushButtonType' : 2,
        }
                )

    oDialog = smgr.createInstance("com.sun.star.awt.UnoControlDialog")
    oDialog.setModel(oDM)
    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 
    oDialog.setPosSize(300,300,300,y+h,POSSIZE)
    oDialog.setVisible(True)
    x = oDialog.execute()
    if x:
        return (txtN.getText(),txtP.getText())
    else:
        return False
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply