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

Keyboard macros or custom scripts
Post Reply
cleanman2
Posts: 36
Joined: Tue Jan 25, 2011 6:21 pm

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

Post 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
OpenOffice 3.2 on Ubuntu
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

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

Post by OlivierR »

Strange request.
Download Grammalecte here:
https://grammalecte.net/#download

Have a look in TextFormatter.py, you should find what you need.
Last edited by OlivierR on Tue Jun 11, 2019 9:22 am, edited 1 time in total.
LibreOffice 6.4Windows 10
cleanman2
Posts: 36
Joined: Tue Jan 25, 2011 6:21 pm

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

Post 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
OpenOffice 3.2 on Ubuntu
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

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

Post 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? :)
LibreOffice 6.4Windows 10
User avatar
Villeroy
Volunteer
Posts: 30676
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Post 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.
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
cleanman2
Posts: 36
Joined: Tue Jan 25, 2011 6:21 pm

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

Post 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
OpenOffice 3.2 on Ubuntu
Post Reply