Programatically add an event to a command button using Python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
gueko1984@gmail.com
Posts: 1
Joined: Mon Nov 27, 2023 9:36 pm

Programatically add an event to a command button using Python

Post by gueko1984@gmail.com »

I want to triger an msgbox when clicking on command button but it doesn't work. In the example in https://help.libreoffice.org/latest/es/ ... tener.html appears the method addActionListener but the commandButton has addEventListener. It works when I place the msgbox in __init__ and disposing but in actionPerformed it doesn't. I'm using LibreOffice 7.3.7.2 and kubuntu. Thanks for your support and help

Code: Select all

import uno
def msgbox(message):
    ctx = uno.getComponentContext()
    sm = ctx.getServiceManager()
    toolkit = sm.createInstanceWithContext('com.sun.star.awt.Toolkit',ctx)
    MsgBox = toolkit.createMessageBox(toolkit.getDesktopWindow(),'QUERYBOX',2,'UNOPython',str(message))
    #mri(toolkit)

    return MsgBox.execute()
def mri(target):

    ctx =  uno.getComponentContext()
    smgr = ctx.getServiceManager()
    oMRI = smgr.createInstance( "mytools.Mri" )
    oMRI.inspect(target )
def addingform():
    from com.sun.star.awt import Point
    from com.sun.star.awt import Size

    oDoc = XSCRIPTCONTEXT.getDocument()

    ctx = uno.getComponentContext()
    sm = ctx.getServiceManager()
    desktop = sm.createInstanceWithContext("com.sun.star.frame.Desktop",ctx)
    doc = desktop.getCurrentComponent()
    doc.getCurrentController().setFormDesignMode(True)
    oSheet = doc.getSheets().getByName("Sheet1")
    oDrawPage = oSheet.getDrawPage()
    oForm = oDoc.createInstance("com.sun.star.form.component.Form")
    oForm.Name = "Form1"
    oForms = oDrawPage.getForms()
    oForms.insertByIndex(0,oForm)
    oButton = doc.createInstance("com.sun.star.form.component.CommandButton")
    oButton.BackgroundColor = 15650253
    oButton.Name = "_MY_BUTTON"
    oButton.Label = "_MY_LABEL"
    #oButton.Enabled = True
    oForm.insertByName("_MY_BUTTON",oButton)
    oShape = doc.createInstance("com.sun.star.drawing.ControlShape")
    oShape.Control = oButton
    oDrawPage.add(oShape)
    oShape.Position  = Point(1000,2000)
    oShape.Size = Size(7560,4000)
    act = ActionListener()

    oShape.addEventListener(act)
    doc.getCurrentController().setFormDesignMode(False)
    #ri(oShape)
    oControl = oForm.getByIndex(0)
#    msgbox(oForm.getScriptEvents(0))

import uno, unohelper
from com.sun.star.awt import XActionListener
from com.sun.star.lang import EventObject
from com.sun.star.awt import ActionEvent
class ActionListener(unohelper.Base, XActionListener):

    def __init__(self):
        self.count = 0


    def disposing(self, EventObject):
        pass

    # XActionListener
    def actionPerformed(self,ActionEvent ):
                                                            
libreoffice 7.3
kubuntu
JeJe
Volunteer
Posts: 2803
Joined: Wed Mar 09, 2016 2:40 pm

Re: Programatically add an event to a command button using Python

Post by JeJe »

The example is a button in a dialog, your button is in the document form - two different types of object. Get MRI and you can inspect that object and see what's available.

viewtopic.php?t=49294
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply