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

Keyboard macros or custom scripts

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

Postby cleanman2 » Fri Sep 12, 2014 9:31 pm

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

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

Postby OlivierR » Sat Sep 13, 2014 11:54 am

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.2Windows 10
OlivierR
 
Posts: 35
Joined: Sun Jul 25, 2010 3:13 pm

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

Postby cleanman2 » Sat Sep 13, 2014 6:30 pm

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

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

Postby OlivierR » Sat Sep 13, 2014 7:17 pm

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.2Windows 10
OlivierR
 
Posts: 35
Joined: Sun Jul 25, 2010 3:13 pm

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

Postby Villeroy » Sat Sep 13, 2014 9:01 pm

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

https://forum.openoffice.org/en/forum/v ... 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, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 27724
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby cleanman2 » Mon Sep 15, 2014 11:03 pm



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


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest