Python...MouseClick in ListBox

Keyboard macros or custom scripts
Post Reply
Posts: 36
Joined: Sun Oct 14, 2012 2:06 pm

Python...MouseClick in ListBox

Post by kiloran »

I'm looking at creating dialogs using Python, and I'm struggling to work out how to handle a mouseclick event.

Here's a stripped-to-basics version of my code:

Code: Select all

# example of creating a dialog using Python
import uno
from import POSSIZE

def addAwtModel(oDM,srv,sName,dProps):
	oCM = oDM.createInstance(""+ srv +"Model")
	while dProps:
		prp = dProps.popitem()
		oCM.Name = sName
def addShareDlg():
	ctx = uno.getComponentContext()
	smgr = ctx.ServiceManager
	oDM = smgr.createInstance("")
	oDM.Title = 'Test'
	oDialog = smgr.createInstance("")
	listText = oDialog.getControl('myListBox')
	listText.setPosSize(10,10,400,300, POSSIZE)    # x, y, width, height, flag
	x = oDialog.execute()
I want to execute some code when I click on an element in the ListBox.
Can someone please point me in the right direction? Many thanks.
LibreOffice 6 on Windows 10 and Linux Mint
User avatar
Posts: 30676
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python...MouseClick in ListBox

Post by Villeroy » ... stBox.html
not sure about the difference between ActionListener and ItemListener. Check this out.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Posts: 36
Joined: Sun Oct 14, 2012 2:06 pm

Re: Python...MouseClick in ListBox

Post by kiloran »

I found some code which implements a listener. It adds a count to a label when a button is clicked.

I then tried adding a ListBox and attached the same listener code to the ListBox.
This essentially works and would form the basis of what I want to do. HOWEVER.... I don't understand why I only need to click once on the button to increment the count, but I need to double-click on an item in the ListBox to achieve the same thing. I would prefer a single-click on the ListBox.

Can anyone provide any guidance

Code: Select all

import uno
import unohelper
from 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():
	"""Opens a dialog with a push button and a label, clicking the button increases the label counter."""
		ctx = uno.getComponentContext()
		smgr = ctx.ServiceManager

		# create the dialog model and set the properties 
		dialogModel = smgr.createInstanceWithContext("", ctx)

		dialogModel.PositionX = 10
		dialogModel.PositionY = 10
		dialogModel.Width = 200 
		dialogModel.Height = 100
		dialogModel.Title = "Runtime Dialog Demo"
		# create listbox
		listBoxModel = dialogModel.createInstance("" )
		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("" )
		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( "" )
		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("", ctx)
		oBox = controlContainer.getControl("myListBoxName")
		oLabel = controlContainer.getControl("myLabelName")
		oButton = controlContainer.getControl("myButtonName")

		# add the action listener
		oButton.addActionListener(MyActionListener( oLabel,labelModel.Label ))
		oBox.addActionListener(MyActionListener( oLabel,labelModel.Label ))
		# create a peer 
		toolkit = smgr.createInstanceWithContext( "", ctx)  

		controlContainer.createPeer(toolkit, None)

		# execute it

		# dispose the dialog 

g_exportedScripts = createDialog,
LibreOffice 6 on Windows 10 and Linux Mint
Post Reply