1

I am creating a for component command button by scripting in python. I fallowed the next tutorial https://help.libreoffice.org/latest/en-GB/text/sbasic/python/python_listener.html except that "com.sun.star.drawing.controlShape doesn't have the method addActionListener but AddEventListener

    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 = "Form2"
    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)


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 actionPerformed(self, ActionEvent):
        msgbox("click")
    def disposing(self, eventObject):
        pass
~                                                                                                                                                                                                                                           
~                         

I put an msgbox in __init__Self() and disposing and they work I want to click the command button and trigger msgbox

1

1 Answer 1

0

According to https://ask.libreoffice.org/t/how-to-programmatically-get-set-a-form-controls-events/45678/3, you need to add the listener to the form rather than to the control. So I came up with the following code.

from com.sun.star.script import ScriptEventDescriptor
oScript = ScriptEventDescriptor()
oScript.ListenerType = "com.sun.star.awt.XMouseListener"
oScript.EventMethod = "mousePressed"
oScript.AddListenerParam = ""
oScript.ScriptType = "Script"
oScript.ScriptCode = "vnd.sun.star.script:action_listener.py$test_message?language=Python&location=user"
oForm.registerScriptEvent(oForm.getCount(), oScript)

However, test_message() didn't get called. Probably there's some mistake, but I didn't want to spend too much time on it.

Note: Instead of form components, there are easier ways to create buttons, such as by inserting hyperlinks into a spreadsheet and then styling them to look like buttons.

Sign up to request clarification or add additional context in comments.

1 Comment

acording to forum.openoffice.org/en/forum/… when you create one commandbutton you need to start in zero oForm.registerScriptEvent(0,oEvent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.