EVT_LEFT_UP event occurring twice?

Hi, I’m new to wxPython and am seeing a few weird things in my small sample program. I am trying to create a clickable label class. macOS Catalina and latest wxPython (python3.8).

  1. When I click to the left of the label (between the label and the window frame - cursor is double-headed arrow), it seems to be sending two click events (counter increases twice).
  2. When I double click in this same spot (when cursor shows the double-headed arrow cursor), the window expands in width (all the way to the left hand side of the screen. Is this expected/documented behaviour?
  3. If I resize the window and then click anywhere inside the frame it “counts” as a click on the label. This seems weird to me as I would expect only a direct click on the label would count as a label click event.

Thanks in advance.

import wx
class ClickableLabel(wx.StaticText):
	def __init__(self, parent, **kwargs):
		super().__init__(parent, **kwargs)
		self.Bind(wx.EVT_LEFT_UP, self.clicked)
		self.click_cnt = 0
	def clicked(self, event):
		self.click_cnt += 1
		self.SetLabel(str(self.click_cnt))

class MyFrame(wx.Frame):
	def __init__(self):
		super().__init__(None, title='Test ClickLabel')
		label = ClickableLabel(self, label='Click Me')
		self.Show()

if __name__ == '__main__':
	app = wx.App(redirect=False)
	frame = MyFrame()
	app.MainLoop()

examplewx

I find it useful in situations like this to change the background color of the widget so it’s easy to see what its bounds are. There are some (probably) surprising behaviors that will become obvious if you do that.

For example, since the frame has only one child then it will automatically resize it to fill the frame:

Snap001

Then, after the clicked method calls SetLabel the wx.StaticText will, by default, resize the widget to fit the new text (if desired you can change this by using the wx.ST_NO_AUTORESIZE style when creating the static text):

Snap002

When the frame is resized then it again resizes it’s only child to fill the frame’s client area:

Snap003

A couple notes:

  1. Using a panel as the frame’s only child, and then using the panel as the parent of the static text will avoid the unexpected resizes.

  2. There is a generic implementation of a static text widget in wx.lib.statbmp. It would probably be better to base your class on it. It’s possible on some platforms that the native wx.StaticText is not a real widget, and so isn’t able to catch mouse events.

1 Like

Hi Robin, thanks for the fast reply and detailed answer. Your note about background color is a good one for my ‘debugging toolbox’. I had a panel but removed it to make my example smaller to post. Looking forward to using wxPython for my future apps.