I was sometimes ago playing around how to interact with classes and their methods. I thought it will be better to share something. Here is simple example to show how to work with methods from another class. This can be helpful in separating GUI from logic (if you don't like xml/xrc) # classes and interactions # this can be good in separating GUI from Logic # Also can be used to separate different classes doing different jobs # programmed by Stephen Mtangoo Email: babaeliya@hotmail.com (United Republic of Tanzania) # Licenced under same LICENCE as wxpython # Anybody can make it better, but don't change the main target: Just improvements are allowed

   1 #!/usr/bin/python
   2 
   3 import wx
   4 
   5 #------------------------------------------------------------------------------
   6 
   7 class AppFrame( wx.Frame ) :
   8     
   9     def __init__( self, title ) :
  10         
  11         wx.Frame.__init__( self, None, -1, title=title )
  12         self.frmPanel = wx.Panel( self )
  13         self.frmPanel.BackgroundColour = (200, 240, 250)    # light blue
  14         
  15         #-----
  16         
  17         # Add another panel and some buttons
  18         self.colourPnl = wx.Panel( self.frmPanel )
  19         self.colourPnl.SetBackgroundColour( wx.NamedColor( 'GREY' ) )
  20         
  21         self.redBtn   = wx.Button( self.frmPanel, label='Red' )
  22         self.greenBtn = wx.Button( self.frmPanel, label='Green' )
  23         self.exitBtn  = wx.Button( self.frmPanel, label='Exit' )
  24         
  25         # Add them to sizer.
  26         colorPnlAndBtn_vSizer = wx.BoxSizer( wx.VERTICAL )
  27         colorPnlAndBtn_vSizer.Add( self.colourPnl, 1, wx.EXPAND|wx.ALL, 1 )
  28         
  29         # Add buttons in their own sizer
  30         btn_hSizer = wx.BoxSizer( wx.HORIZONTAL )
  31         btn_hSizer.AddStretchSpacer()
  32         btn_hSizer.Add( self.redBtn,   proportion=0, flag=wx.EXPAND|wx.ALL, border=5 )
  33         btn_hSizer.Add( self.greenBtn, proportion=0, flag=wx.EXPAND|wx.ALL, border=5 )
  34         btn_hSizer.Add( self.exitBtn,  proportion=0, flag=wx.EXPAND|wx.ALL, border=5 )
  35         btn_hSizer.AddStretchSpacer()
  36         
  37         colorPnlAndBtn_vSizer.Add( btn_hSizer, 0, wx.EXPAND| wx.ALL, 5 )
  38         
  39         # SetSizer both sizers in the most senior control that has sizers in it.
  40         self.frmPanel.SetSizer( colorPnlAndBtn_vSizer )
  41         self.frmPanel.Layout()
  42         
  43         #-----
  44         
  45         # Must call before any event handler is referenced.
  46         self.eventsHandler = EventsHandler( self )
  47         
  48         # Bind event handlers to all controls that have one.
  49         self.redBtn.  Bind( wx.EVT_BUTTON, self.eventsHandler.OnRedBtn )
  50         self.greenBtn.Bind( wx.EVT_BUTTON, self.eventsHandler.OnGreenBtn )
  51         self.exitBtn. Bind( wx.EVT_BUTTON, self.eventsHandler.OnExitBtn )
  52         
  53         # Create more convenient ways to close this app.
  54         # Adding these makes a total of 5 separate ways to exit.
  55         self.frmPanel .Bind( wx.EVT_LEFT_DCLICK, self.eventsHandler.OnExitBtn )
  56         self.colourPnl.Bind( wx.EVT_LEFT_DCLICK, self.eventsHandler.OnExitBtn )
  57         
  58     #end __init__
  59     
  60 #end AppFrame class
  61 
  62 #------------------------------------------------------------------------------
  63 
  64 class EventsHandler() :
  65     
  66     def __init__( self, parent ) :
  67         self.parent = parent
  68 
  69     def OnRedBtn( self, event ) :
  70         self.ShowColourAndDialog( wx.RED )
  71 
  72     def OnGreenBtn( self, event ) :
  73         self.ShowColourAndDialog( wx.GREEN )
  74         
  75     def ShowColourAndDialog( self, pnlColour ) :
  76         
  77         self.parent.colourPnl.SetBackgroundColour( pnlColour )
  78         self.parent.colourPnl.Refresh()
  79         dlg = wx.MessageDialog( self.parent, 'Changed colour !', 'Successful', 
  80                                 style = wx.ICON_INFORMATION| wx.OK )
  81         dlg.ShowModal()
  82         dlg.Destroy()
  83 
  84     def OnExitBtn( self, event ) :
  85         self.parent.Destroy()
  86     
  87 #end Events class
  88 
  89 #------------------------------------------------------------------------------
  90 
  91 def Main() :
  92     
  93     app = wx.App( redirect=False )
  94     appFrm = AppFrame( title='Separate the Gui from the Logic' )
  95     appFrm.Show()
  96     app.MainLoop()
  97 
  98 #end class
  99 
 100 #==============================================================================
 101 
 102 if __name__ == '__main__' :
 103     
 104     Main()


SeparateGuiAndLogic (last edited 2011-06-16 23:19:17 by pool-71-244-98-82)

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