Interface-oriented programming in OpenOffice / LibreOffice : automate your office tasks with Python Macros
ListBox,Button,Labelの内、ListBoxは作成しているだけで操作は出来ません。
「Click Me」ボタンをクリックするたびにClicks 1・2・・・と数字が増えていきます。
import uno
import unohelper
from com.sun.star.awt import XActionListener
class MyActionListener( unohelper.Base, XActionListener ):
def __init__(self, labelControl, prefix ):
self.nCount = 0
self.labelControl = labelControl
self.prefix = prefix
def actionPerformed(self, actionEvent):
# increase click counter
self.nCount = self.nCount + 1
self.labelControl.setText( self.prefix + str( self.nCount ) )
def createDialog():
# create dialog
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
dialogModel = smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialogModel", ctx)
dialogModel.PositionX = 10
dialogModel.PositionY = 10
dialogModel.Width = 200
dialogModel.Height = 100
dialogModel.Title = "Runtime Dialog Demo"
# create listbox
listBoxModel = dialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel" )
listBoxModel.PositionX = 10
listBoxModel.PositionY = 5
listBoxModel.Width = 100
listBoxModel.Height = 40
listBoxModel.Name = "myListBoxName"
listBoxModel.StringItemList = ('a','b','c')
# create the button model and set the properties
buttonModel = dialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel" )
buttonModel.PositionX = 50
buttonModel.PositionY = 50
buttonModel.Width = 50
buttonModel.Height = 14
buttonModel.Name = "myButtonName"
buttonModel.Label = "Click Me"
# create the label model and set the properties
labelModel = dialogModel.createInstance( "com.sun.star.awt.UnoControlFixedTextModel" )
labelModel.PositionX = 10
labelModel.PositionY = 70
labelModel.Width = 100
labelModel.Height = 14
labelModel.Name = "myLabelName"
labelModel.Label = "Clicks "
# insert the control models into the dialog model
dialogModel.insertByName( "myButtonName", buttonModel)
dialogModel.insertByName( "myLabelName", labelModel)
dialogModel.insertByName( "myListBoxName", listBoxModel)
# create the dialog control and set the model
controlContainer = smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialog", ctx)
controlContainer.setModel(dialogModel)
oBox = controlContainer.getControl("myListBoxName")
oLabel = controlContainer.getControl("myLabelName")
oButton = controlContainer.getControl("myButtonName")
oBox.addItem('d',4)
# create a peer
toolkit = smgr.createInstanceWithContext( "com.sun.star.awt.ExtToolkit", ctx)
controlContainer.setVisible(False)
controlContainer.createPeer(toolkit, None)
# add the action listener
oButton.addActionListener(MyActionListener( oLabel,labelModel.Label ))
oBox.addActionListener(MyActionListener( oLabel,labelModel.Label ))
# execute again
controlContainer.execute()
controlContainer.dispose()
return None
g_exportedScripts = createDialog,
上記のマクロを
/home/ユーザ名/.config/libreoffice/4/user/Scripts/python/ (Ubuntuの場合)
C:\Users\ユーザ名\AppData\Roaming\LibreOffice\4\user\Scripts\python (Windowsの場合)
に保存する。
その他の参考にしたサイト
Python で OpenOfficeマクロ
ダイアログ・エディタの利用とコード上でのイベント関連づけ
http://psyto.s26.xrea.com/python/ooomacro_dialog.html
Apache OpenOffice wiki
List Box
https://wiki.openoffice.org/wiki/Documentation/DevGuide/Basic/List_Box
Apache OpenOffice
service UnoControlListBox
http://www.openoffice.org/api/docs/common/ref/com/sun/star/awt/UnoControlListBox.html
Apache OpenOffice
interface XListBox
http://www.openoffice.org/api/docs/common/ref/com/sun/star/awt/XListBox.html