Coding event handlers

In your wxPython code, events and event handlers are managed on a widget-by-widget basis. For example, a button click is dispatched to a particular handler based on the button that was clicked. In order to bind an event from a specific widget to a specific handler method, you use a binder object to manage the connection. For example, self.Bind(wx.EVT_BUTTON, self.OnClick, aButton)

uses the predefined event binder object wx.evt_button to associate a button click event on the object aButton with the method self.OnClick. The Bind() method is a method of wx.EvtHandler, which is a parent class of all display objects. Therefore, the example line of code can be placed in any display class.

Even as your wxPython program appears to be waiting passively for an event, it's still doing something. Specifically, it's running the method wx.App.Main-Loop(), which is basically an infinite while loop. The MainLoop() can be translated into oversimplified Python pseudocode as:

while True:

while not self.Pending():

self.ProcessIdle() self.DoMessage()

In other words, if there is no message pending, do some idle processing until a message comes in, then dispatch the message to the appropriate event-handling method.

Was this article helpful?

0 0

Post a comment