Attachment 'bindings_demo.py'

Download

   1 #!/usr/bin/python
   2 # -*- encoding: utf-8
   3 
   4 """ A demo to illustrate event handler search and execution hierarchy. """
   5 
   6 import sys, os
   7 import wx
   8 
   9 #------------------------------------------------------------------------------
  10 
  11 class MainFrame( wx.Frame ) :
  12     
  13     def __init__( self, title='Event Handler Hierarchy', pos=(10, 10), size=(350, 200) ) :
  14         
  15         wx.Frame.__init__( self, None, id=-1, title=title, pos=pos, size=size )
  16         
  17         #----- Frame's controls:
  18         
  19         # An initial Frame panel is needed for tab-traversal 
  20         #   and platform background color capabilities.
  21         # The first control instantiated in a Frame automatically expands 
  22         #   to the extent of the Frame's client area. This is unique to Frames.
  23         frm_pnl = wx.Panel( self )
  24         
  25         btn1 = wx.Button( frm_pnl, label='Btn1' )
  26         btn2 = wx.Button( frm_pnl, label='Btn2' )
  27         
  28         #----- Button centering:
  29         
  30         # Automatically center the button position as simply as possible.
  31         pnl_horzSizer = wx.BoxSizer( wx.HORIZONTAL )
  32         pnl_horzSizer.AddStretchSpacer()
  33         pnl_horzSizer.Add( btn1, flag=wx.ALIGN_CENTER )  # Center horizontally.
  34         pnl_horzSizer.Add( btn2, flag=wx.ALIGN_CENTER )  # Center horizontally.
  35         pnl_horzSizer.AddStretchSpacer()    # 2# StretchSpacers assure btn's vertical centering.
  36         
  37         frm_pnl.SetSizer( pnl_horzSizer )
  38         frm_pnl.Layout()
  39         
  40         #----- Buttons' click event handlers:
  41         
  42         ## Comment out or un-comment these handler bindings 
  43         #   to see the effects of each remaining combination.
  44         
  45         #-----
  46         
  47         # Bind the button-to-Frame handler. 
  48         # The 3rd argument specifies which button's events to handle.
  49         self.Bind( wx.EVT_BUTTON, self.OnButton_FrameHandler, btn1 )
  50         
  51         ## Bind both button's events because there is no 3rd argument.
  52         #self.Bind( wx.EVT_BUTTON, self.OnButton_FrameHandler )
  53         
  54         #-----
  55         
  56         # Bind the button-to-Panel handler.
  57         frm_pnl.Bind( wx.EVT_BUTTON, self.OnButton_PanelHandler, btn1 )
  58         
  59         ## Bind both button's events because there is no 3rd argument.
  60         #frm_pnl.Bind( wx.EVT_BUTTON, self.OnButton_PanelHandler )
  61         
  62         #-----
  63         
  64         # Bind the button's own handler. No 3rd argument is necessary because "btn1.Bind".
  65         btn1.Bind( wx.EVT_BUTTON, self.OnButton_ButtonHandler )
  66     
  67     #end __init__
  68     
  69     #------------------------
  70     
  71     def OnButton_FrameHandler( self, event ) :
  72         # The button that generated this event:
  73         btn = event.GetEventObject()
  74         print '\n----  OnButton_FrameHandler() for', btn.GetLabelText()
  75         
  76         # There is nowhere to .Skip() up to.
  77     #end def
  78     
  79     def OnButton_PanelHandler( self, event ) :
  80         # The button that generated this event:
  81         btn = event.GetEventObject()
  82         print '\n----  OnButton_PanelHandler() for', btn.GetLabelText()
  83         
  84         event.Skip()    # Search for handler upwards in the child-parent hierarchy tree.
  85     #end def
  86     
  87     def OnButton_ButtonHandler( self, event ) :
  88         # The button that generated this event:
  89         btn = event.GetEventObject()
  90         print '\n----  OnButton_ButtonHandler() for', btn.GetLabelText()
  91         
  92         event.Skip()    # Search for handler upwards in the child-parent hierarchy tree.
  93     #end def
  94     
  95 #end class
  96 
  97 #==============================================================================
  98 
  99 if __name__ == '__main__' :
 100     app = wx.PySimpleApp( redirect=False )
 101     appFrame = MainFrame().Show()
 102     app.MainLoop()
 103 #end def

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2010-11-05 01:25:47, 3.7 KB) [[attachment:bindings_demo.py]]
  • [get | view] (2009-10-04 09:16:02, 42.1 KB) [[attachment:wxpython_events_and_apples.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.