Event triggers

In wxPython, most widgets cause higher level events to be fired in response to lower level events. For example, a mouse click within the space of a wx.Button causes the generation of an EVT_BUTTON event, which is a specific type of wx.Command-Event. Similarly, a mouse drag in the corner of a window causes a wx.SizeEvent to be created automatically for you by wxPython. The advantage of these higher level events is that they make it easier for the rest of your system to focus on the most relevant events, rather than getting bogged down in tracking every mouse click. For example, saying that a mouse click is a button activation makes it clear that a particular click has contextual meaning in the system, whereas another mouse click may not contain contextual meaning. Higher level events can also encapsulate more useful information about the event. As you create your own custom widgets, you can define your own custom events to manage this process for you.

Events are represented within wxPython by objects. Specifically, event objects in wxPython are instances of the class wx.Event, or one of its subclasses. The parent wx.Event class is a relatively small abstract class consisting of getters and setters for a few properties common to all events, such as EventType, EventObject, and Timestamp. Different subclasses of wx.Event each add further information. For example, wx.MouseEvent contains information about the exact location of the mouse as the event happened, and information about which mouse button was clicked, if any.

There are several different subclasses of wx.Event in wxPython. Table 3.2 contains a list of some of the event classes you will most often encounter. Remember, one event class can have multiple event types, each corresponding to a different user action.

Table 3.2 Important subclasses of wx.Event




Triggered when a frame closes. The event type distinguishes between a normal frame closing and a system shutdown event.


Triggered by a wide variety of simple interactions with widgets, such as a button click, menu item selection, or radio button selection. Each of these separate actions has its own event type. Many more complex widgets, such as the list or grid controls, define subclasses of wx.CommandEvent. Command events are treated differently by the event handling system than by other events.

continued on next page continued on next page

Table 3.2 Important subclasses of wx.Event (continued)




A key press event. The event types distinguish between key down, key up, and complete key press.


A mouse event. The event types distinguish between a mouse move and a mouse click. There are separate event types depending on which button is clicked and whether it's a single or double click.


Triggered when a window's contents need to be redrawn.


This event is triggered when a window is resized, and typically results in a change to the window layout.


Can be created by the wx.Timer class, which allows periodic events.

Typically, event objects do very little on their own, but instead, need to be passed to the relevant event handler method or methods using an event binder and an event processing system.

Was this article helpful?

0 0

Post a comment