Page 1 of 1

Looking for an example in Python for a 2-button dialog

Posted: Fri Sep 12, 2014 9:31 pm
by cleanman2
I am trying to rewrite some basic macros in Python. Right now I am experimenting with using a dialog designed in basic dialog editor in a Python macro. I have found some code I adapted to use an ActionListener to respond to one button on the dialog but am completely lost on how to use a second button on the same dialog.

Could anyone poiint me to a code example that demonstrates this type of usage?

Thanks, Jim

Re: Looking for an example in Python for a 2-button dialog

Posted: Sat Sep 13, 2014 11:54 am
by OlivierR
Strange request.
Download Grammalecte here:
https://grammalecte.net/#download

Have a look in TextFormatter.py, you should find what you need.

Re: Looking for an example in Python for a 2-button dialog

Posted: Sat Sep 13, 2014 6:30 pm
by cleanman2
Thanks. Looking at TextFormatter.py allowed me to get both buttons working.

I am curious about what seemed strange in the request.

Thanks again, Jim

Re: Looking for an example in Python for a 2-button dialog

Posted: Sat Sep 13, 2014 7:17 pm
by OlivierR
I was just wondering what might prevent you to do one more time what you succeeded to do once. I thought it was possibly related to widget naming but it seemed too obvious.

BTW, I’m interested to try dialog design by editor and controlling it via Python. Do you have some code to publish about that? :)

Re: Looking for an example in Python for a 2-button dialog

Posted: Sat Sep 13, 2014 9:01 pm
by Villeroy
cleanman2 wrote:Could anyone poiint me to a code example that demonstrates this type of usage?
viewtopic.php?f=20&t=66391&p=295703&hil ... ON#p295691 relates to an xdl file of an extension. A simple file:///path/dialog.xdl may work as well.

Re: Looking for an example in Python for a 2-button dialog

Posted: Mon Sep 15, 2014 11:03 pm
by cleanman2


I was just wondering what might prevent you to do one more time what you succeeded to do once. I thought it was possibly related to widget naming but it seemed too obvious.
Well that is pretty close to being true. All of the examples I was finding only had one button and I could not tell from them how to identify individual buttons to attach code to their events. Until I saw your code I did not know about setActionCommand() and ActionCommand after that it all made sense.

My code is far from done but this minimal example will pop up a dialog and take action depending on which button is clicked. The dialog (in this case enter_pmts.dialog) was designed in the dialog portion of the basic IDE and saved with the spreadsheet in which it will be used. You can look up the various properties in the IDE and use them in your Python code.

Code: Select all

class ButtonListen( unohelper.Base, XActionListener ):
    def __init__(self, oControl, oButton, oDialog):
        self.textField = oControl
        self.button = oButton
        self.dialog = oDialog
    
    #actionPerformed not to be renamed
    def actionPerformed(self, XActionEvent):  # actionEvent)
        if XActionEvent.ActionCommand == 'OK':
            omsgbox(self.textField.getText())
        elif XActionEvent.ActionCommand == 'Cancel':
            self.dialog.endExecute()

def test_dlg():
    doc = XSCRIPTCONTEXT.getDocument()
    ctx = XSCRIPTCONTEXT.getComponentContext()
    sm = ctx.ServiceManager
    dp = sm.createInstanceWithArgumentsAndContext(
       "com.sun.star.awt.DialogProvider", (doc, ), ctx)
    dlg = dp.createDialog("vnd.sun.star.script:Standard.enter_pmts_dialog?location=document")
    textField1_ctrl = dlg.getControl('textField1')
    ok_btn_ctrl = dlg.getControl('ok_Btn')
    ok_btn_listener = ButtonListen(textField1_ctrl, ok_btn_ctrl, dlg)
    ok_btn_ctrl.addActionListener(ok_btn_listener)
    ok_btn_ctrl.setActionCommand('OK')
    cancel_btn_ctrl = dlg.getControl('cancel_Btn')
    cancel_btn_listener = ButtonListen(textField1_ctrl, cancel_btn_ctrl, dlg)
    cancel_btn_ctrl.addActionListener(cancel_btn_listener)
    cancel_btn_ctrl.setActionCommand('Cancel')
    dlg.execute()
    dlg.dispose() 
    
def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
    #shows message. Used for debugging 
    desktop = XSCRIPTCONTEXT.getDesktop()
    frame = desktop.getCurrentFrame() 
    window = frame.getContainerWindow()
    toolkit = window.getToolkit()
    msgbox = toolkit.createMessageBox(window, Rectangle(), oMsgType, oBtnType, oTitle, oMessage)
    return msgbox.execute()
 
Regards, Jim