DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

wxPython: Binding Multiple Widgets to the Same Handler

Mike Driscoll user avatar by
Mike Driscoll
·
Feb. 18, 12 · Interview
Like (0)
Save
Tweet
Share
7.02K Views

Join the DZone community and get the full member experience.

Join For Free

If you’ve been in the wxPython community for more than a couple months, you will probably recognize the following question: “How do I bind multiple buttons to the same event handler and make them do different things?” Well, this article will show you how to do exactly that.

Note: This article is based on some code from a previous article on buttons from this very blog!

Let’s Get Started

To begin, we need to write some code that actually contains multiple buttons. We will go through an example that shows two different ways to get the button object so you can manipulate your program as needed. Here’s the code you’ve been waiting for:

import wx
 
########################################################################
class MyForm(wx.Frame):
 
    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Button Tutorial")
        panel = wx.Panel(self, wx.ID_ANY)
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        buttonOne = wx.Button(panel, label="One", name="one")
        buttonTwo = wx.Button(panel, label="Two", name="two")
        buttonThree = wx.Button(panel, label="Three", name="three")
        buttons = [buttonOne, buttonTwo, buttonThree]
 
        for button in buttons:
            self.buildButtons(button, sizer)
 
        panel.SetSizer(sizer)
 
    #----------------------------------------------------------------------
    def buildButtons(self, btn, sizer):
        """"""
        btn.Bind(wx.EVT_BUTTON, self.onButton)
        sizer.Add(btn, 0, wx.ALL, 5)
 
    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        This method is fired when its corresponding button is pressed
        """
        button = event.GetEventObject()
        print "The button you pressed was labeled: " + button.GetLabel()
        print "The button's name is " + button.GetName()
 
        button_id = event.GetId()
        button_by_id = self.FindWindowById(button_id)
        print "The button you pressed was labeled: " + button_by_id.GetLabel()
        print "The button's name is " + button_by_id.GetName()
 
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()


To start, we create three button objects. Then to make things slightly less messy, we put them into a list and iterate over the list to add the buttons to a sizer and bind them to an event handler. This is a good way to cut down on spaghetti code (i.e. copy and pasted code) and makes it a little cleaner and easier to debug. Some people go ahead and create some elaborate helper methods like buildButtons that can handle other widgets and are more flexible.

The part we really care about though is the event handler itself. The easiest way to get the widget in the event handler is by calling the event object’s GetEventObject() method. That will return the widget and then you can do whatever you like. Some people will change the widget’s value or label, others will use the widgets ID or unique name and set up some conditional structures to do something if this button is pressed and do something else if a different button is pressed. The functionality is up to you.

The second way to get the widget is a two-step process where we need to extract the ID from the event using its GetID() method. Then we pass that result to our frame object’s FindWindowById() method and we once again have the widget in question.

Wrapping up

Now you know the “secret” of binding multiple widgets to the same event handler. Go forth and code like there’s no tomorrow and create something amazing! The code can be downloaded on the new blog’s Mercurial repository.

Additional Resources

  • wxPython: A Tour of Buttons (Part 1 of 2)

  • self.Bind vs. self.button.Bind

  • wx.Button documentation

  • Creating buttons tutorial on Youtube



Source: http://www.blog.pythonlibrary.org/2011/09/20/wxpython-binding-multiple-widgets-to-the-same-handler/

Binding (linguistics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices 101: Transactional Outbox and Inbox
  • Fixing Bottlenecks in Your Microservices App Flows
  • Practical Example of Using CSS Layer
  • Mission-Critical Cloud Modernization: Managing Coexistence With One-Way Data Sync

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: