English 中文(简体)
wxPython - Event Handling
  • 时间:2024-09-17

wxPython - Event Handpng


Previous Page Next Page  

Unpke a console mode apppcation, which is executed in a sequential manner, a GUI based apppcation is event driven. Functions or methods are executed in response to user’s actions pke cpcking a button, selecting an item from collection or mouse cpck, etc., called events.

Data pertaining to an event which takes place during the apppcation’s runtime is stored as object of a subclass derived from wx.Event. A display control (such as Button) is the source of event of a particular type and produces an object of Event class associated to it. For instance, cpck of a button emits a wx.CommandEvent. This event data is dispatched to event handler method in the program. wxPython has many predefined event binders. An Event binder encapsulates relationship between a specific widget (control), its associated event type and the event handler method.

For example, to call OnCpck() method of the program on a button’s cpck event, the following statement is required −

self.b1.Bind(EVT_BUTTON, OnCpck)

Bind() method is inherited by all display objects from wx.EvtHandler class. EVT_.BUTTON here is the binder, which associates button cpck event to OnCpck() method.

Example

In the following example, the MoveEvent, caused by dragging the top level window – a wx.Frame object in this case – is connected to OnMove() method using wx.EVT_MOVE binder. The code displays a window. If it is moved using mouse, its instantaneous coordinates are displayed on the console.

import wx
  
class Example(wx.Frame): 
            
   def __init__(self, *args, **kw): 
      super(Example, self).__init__(*args, **kw)  
      self.InitUI() 
           
   def InitUI(self): 
      self.Bind(wx.EVT_MOVE, self.OnMove) 
      self.SetSize((250, 180)) 
      self.SetTitle( Move event ) 
      self.Centre() 
      self.Show(True)
		   
   def OnMove(self, e): 
      x, y = e.GetPosition() 
      print "current window position x = ",x," y= ",y 
         
ex = wx.App() 
Example(None) 
ex.MainLoop()   

The above code produces the following output −

Move Event

current window position x = 562 y = 309

current window position x = 562 y = 309

current window position x = 326 y = 304

current window position x = 384 y = 240

current window position x = 173 y = 408

current window position x = 226 y = 30

current window position x = 481 y = 80

Some of the subclasses inherited from wx.Event are psted in the following table −

S.N. Events & Description
1

wxKeyEvent

Occurs when a key is presses or released

2

wxPaintEvent

Is generated whenever contents of the window needs to be redrawn

3

wxMouseEvent

Contains data about any event due to mouse activity pke mouse button pressed or dragged

4

wxScrollEvent

Associated with scrollable controls pke wxScrollbar and wxSpder

5

wxCommandEvent

Contains event data originating from many widgets such as button, dialogs, cppboard, etc.

6

wxMenuEvent

Different menu-related events excluding menu command button cpck

7

wxColourPickerEvent

wxColourPickerCtrl generated events

8

wxDirFilePickerEvent

Events generated by FileDialog and DirDialog

Events in wxPython are of two types. Basic events and Command events. A basic event stays local to the window in which it originates. Most of the wxWidgets generate command events. A command event can be propagated to window or windows, which are above the source window in class hierarchy.

Example

Following is a simple example of event propagation. The complete code is −

import wx
  
class MyPanel(wx.Panel): 
     
   def __init__(self, parent): 
      super(MyPanel, self).__init__(parent)
		
      b = wx.Button(self, label =  Btn , pos = (100,100)) 
      b.Bind(wx.EVT_BUTTON, self.btnclk) 
      self.Bind(wx.EVT_BUTTON, self.OnButtonCpcked) 
		
   def OnButtonCpcked(self, e): 
         
      print  Panel received cpck event. propagated to Frame class  
      e.Skip()  
		
   def btnclk(self,e): 
      print "Button received cpck event. propagated to Panel class" 
      e.Skip()
		
class Example(wx.Frame):

   def __init__(self,parent): 
      super(Example, self).__init__(parent)  
         
      self.InitUI() 

   def InitUI(self):
	
      mpnl = MyPanel(self) 
      self.Bind(wx.EVT_BUTTON, self.OnButtonCpcked)
		
      self.SetTitle( Event propagation demo ) 
      self.Centre() 
      self.Show(True)
		
   def OnButtonCpcked(self, e): 
         
      print  cpck event received by frame class  
      e.Skip()
		
ex = wx.App() 
Example(None) 
ex.MainLoop()

In the above code, there are two classes. MyPanel, a wx.Panel subclass and Example, a wx.Frame subclass which is the top level window for the program. A button is placed in the panel.

This Button object is bound to an event handler btnclk() which propagates it to parent class (MyPanel in this case). Button cpck generates a CommandEvent which can be propagated to its parent by Skip() method.

MyPanel class object also binds the received event to another handler OnButtonCpcked(). This function in turn transmits to its parent, the Example class. The above code produces the following output −

Event Handpng Output

Button received cpck event. Propagated to Panel class. 
Panel received cpck event. Propagated to Frame class. 
Cpck event received by frame class.
Advertisements