Page 1 of 1
[Solved] Dialog box in Python
Posted: 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 12235 times
Thanks
Re: Dialog box in Python
Posted: Thu Sep 20, 2012 8:34 pm
by Villeroy
Re: Dialog box in Python
Posted: Thu Sep 20, 2012 8:38 pm
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()
Re: Dialog box in Python
Posted: 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
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,
Re: Dialog box in Python
Posted: Fri Sep 21, 2012 6:16 pm
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=
Re: Dialog box in Python
Posted: 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
Posted: 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 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
Re: [Solved] Dialog box in Python
Posted: 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
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