What is the best way to establish a wxPython event handler for a mouse click, button and so on? I have looked at a number of sample codes and the event handlers differ all over the place.

Recommended Answers

All 5 Replies

I have looked up some of the wxPython event handlers I have used over time. You are right there are different versions, some are outdated, some are personal preference. I prefer the version used by button4 or the mouse clicks on the parent frame ...

# checking different versions of the wxPython event handler

import wx

ID_BTN1 = 1001  # give button1 a unique id number
ID_BTN2 = 1002

class MyFrame(wx.Frame):
    """make a frame with test buttons, inherits wx.Frame"""
    def __init__(self):
        # create a frame/window, no parent, default to wxID_ANY
        wx.Frame.__init__(self, None, wx.ID_ANY, 'wxPython', pos=(200, 150), size=(320, 250))
        self.SetBackgroundColour('green')
        
        self.button1 = wx.Button(self, id=ID_BTN1, label='Button1', pos=(8, 8), size=(175, 28))
        # older style event handler using the button's id number
        wx.EVT_BUTTON(self, ID_BTN1, self.button1Click)

        self.button2 = wx.Button(self, id=ID_BTN2, label='Button2', pos=(8, 38), size=(175, 28))
        # newer style event handler (wxPython version 2.5 and higher) using Bind() and button's id
        self.Bind(wx.EVT_BUTTON, self.button2Click, id=ID_BTN2)
        
        # button uses the default id of -1
        self.button3 = wx.Button(self, id=-1, label='Button3', pos=(8, 68), size=(175, 28))
        # another version of Bind() identifying the button by name
        self.Bind(wx.EVT_BUTTON, self.button3Click, self.button3)
        
        self.button4 = wx.Button(self, id=-1, label='Button4', pos=(8, 98), size=(175, 28))
        # one more version if Bind() identifying the button up front
        self.button4.Bind(wx.EVT_BUTTON, self.button4Click)

        # handle mouse events on frame
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
        
        # show the frame
        self.Show(True)
        
    def button1Click(self, event):
        self.SetTitle("Button1 clicked")
        
    def button2Click(self, event):
        self.SetTitle("Button2 clicked")
        
    def button3Click(self, event):
        self.SetTitle("Button3 clicked")

    def button4Click(self, event):
        self.SetTitle("Button4 clicked")

    def OnLeftDown(self, event):
        pt = event.GetPosition()  # position tuple
        self.SetTitle('left mouse = ' + str(pt))
        
    def OnRightDown(self, event):
        pt = event.GetPosition()
        self.SetTitle('right mouse = ' + str(pt))


application = wx.PySimpleApp()
# call class MyFrame
window = MyFrame()
# start the event loop
application.MainLoop()

Thanks Vega,
I can use your code sample as a template, as I play around with the capabilities of the wxPython GUI.

You have used the frame directly to put your button widgets on. Other code samples seem to use a panel on a frame to put their widgets on. Any reason for that?

Also, why are you using self.button1 = Button( ... ) and not just button1 = Button( ... )?

...
Also, why are you using self.button1 = Button( ... ) and not just button1 = Button( ... )?

The self. prefix makes the button global within the class, so it can be used by methods of the class.

Panels have some added flexibilty over just frames, things like sizers.

Thanks, I will check the panels and the sizers out next!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.