我正在通过Python脚本创建一个for组件命令按钮。我休整了下一个教程https://help.libreoffice.org/latest/en-GB/text/sbasic/python/python_listener.html除了“com.sun.star.drawing.controlShape没有该方法addActionListener 但 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
~
~
我在 __init__Self() 中放入了一个 msgbox 并进行了处理,它们就起作用了 我想点击命令按钮并触发msgbox
根据https://ask.libreoffice.org/t/how-to-programmatically-get-set-a-form-controls-events/45678/3,您需要将侦听器添加到表单而不是到控制。所以我想出了以下代码。
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)
然而,
test_message()
并没有被叫到。可能有一些错误,但我不想花太多时间在上面。
注意:除了表单组件之外,还有更简单的方法来创建按钮,例如将超链接插入到电子表格中,然后将其样式设置为看起来像按钮。