Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
wxPython Application Development Cookbook

You're reading from   wxPython Application Development Cookbook Over 80 step-by-step recipes to get you up to speed with building your own wxPython applications

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785287732
Length 264 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Cody Precord Cody Precord
Author Profile Icon Cody Precord
Cody Precord
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. wxPython Starting Points FREE CHAPTER 2. Common User Controls 3. UI Layout and Organization 4. Containers and Advanced Controls 5. Data Displays and Grids 6. Ways to Notify and Alert 7. Requesting and Retrieving Information 8. User Interface Primitives 9. Creating and Customizing Components 10. Getting Your Application Ready for Release Index

Binding to events

wxPython is an event-driven framework; this means that all actions and the running of the UI is driven by events. Events are fired by objects to indicate that something has happened or needs to happen. MainLoop then dispatches these events to callback methods that are registered to be notified of the event. This recipe will show how to bind callback functions to events.

How to do it…

Perform the following functions:

  1. First, start by creating a frame and binding to some of its events with the following code:
    class MyApp(wx.App):
        def OnInit(self):
            self.frame = wx.Frame(None, title="Binding Events")
            
            # Bind to events we are interested in
            self.frame.Bind(wx.EVT_SHOW, self.OnFrameShow)
            self.frame.Bind(wx.EVT_CLOSE, self.OnFrameExit)
            
            # Show the frame
            self.frame.Show()
            return True
  2. Next, define the event handler callback methods we specified in the Bind calls. These will get executed when the bound event occurs, as follows:
        def OnFrameShow(self, event):
            theFrame = event.EventObject
            print("Frame (%s) Shown!" % theFrame.Title)
            event.Skip()
    
        def OnFrameExit(self, event):
            theFrame = event.EventObject
            print("Frame (%s) is closing!" % theFrame.Title)
            event.Skip()

How it works…

In the OnInit method, we created a frame object and then called Bind on it two times in order to bind our own two callback methods to these events that the frame emits. In this case, we bound to EVT_SHOW and EVT_CLOSE; these two events will be emitted by a window when the window transitions from being hidden to shown on screen and then when it is closed. Binding to events allows us to add some application-specific response when these two events occur. Now, our app's OnFrameShow and OnFrameExit callbacks will be executed by the framework in response to the event and allow us to print our log messages.

The first event, EVT_SHOW, happens as part of when the Show method is called on the frame in the app's OnInit method. The other event, EVT_CLOSE, occurs when the frame's close button is clicked on.

The event handler methods used in Bind always take one argument, which is an event object. This object is passed into the handler by the framework when it is called. The event object contains information about the event, such as a reference to the object that emitted it and other state information depending on what type of event was emitted.

There's more…

The Bind function can also take some additional optional parameters to set more fine-grain control on when the callback should be executed, as follows:

Bind(event, handler, source=None, id=-1, id2=-1)

The arguments to this function are described as follows:

  • event: This is the event to bind to.
  • handler: This is the event handler callback function to bind.
  • source: This can be used to specify the window object that is the source of the event. If specified, then only when the source object generates the event will the handler be executed. By default, any event of the type that gets to the control will cause the handler to execute.
  • id1: This is used to specify the source object's ID instead of using the instance.
  • id2: When specified with id1, this can be used to specify a range of IDs to bind to.

There are many kinds of events that can be bound to depending on the type of control. The wxPython and wxWidgets online documentation provides a fairly complete list of events that are available for each control in the library. Note that the documentation is based on object hierarchy, so you may have to look to the base classes of an object to find the more general events that many controls share. You can find the documentation at http://wxpython.org/onlinedocs.php.

See also

  • Take a look at the Controlling the propagation of events recipe section in this chapter for information on the behavior of events.
You have been reading a chapter from
wxPython Application Development Cookbook
Published in: Dec 2015
Publisher:
ISBN-13: 9781785287732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image