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 meaning/theme: Just improvements are allowed
#!/usr/bin/python
import wx
class GUI(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        self.functions = Events(self)
        self.panel = wx.Panel(self, -1)
        # add two buttons and text control
        self.text = wx.TextCtrl(self.panel, -1)
        self.text.SetBackgroundColour(wx.BLACK)
        self.button1 = wx.Button(self.panel, -1, "Red Colour")
        self.button2 = wx.Button(self.panel, -1, "Green Colour")
        #bind events to widgets
        self.Bind(wx.EVT_BUTTON, self.functions.onRed, id = self.button1.GetId())
        self.Bind(wx.EVT_BUTTON, self.functions.onGreen, id = self.button2.GetId())
        # add them to sizer [define it first to prevent python automatic detector to find you red handed]
        main = wx.BoxSizer(wx.VERTICAL)
        main.Add(self.text, 1, wx.EXPAND|wx.ALL, 1)
        #add buttons in separate sizer
        sub_main = wx.BoxSizer(wx.HORIZONTAL)
        sub_main.Add(self.button1, 0, wx.EXPAND| wx.ALL, 5)
        sub_main.Add(self.button2, 0, wx.EXPAND| wx.ALL, 5)
        main.Add(sub_main, 0, wx.EXPAND| wx.ALL, 5)
        #set sizers to Frame through panel
        self.panel.SetSizer(main)
        self.panel.Layout()
        
class Events():
    def __init__(self, parent):
        self.gui = parent
        
    def onRed(self, evt):
        self.gui.text.SetBackgroundColour(wx.RED)
        dlg = wx.MessageDialog(self.gui, "Changed colour!. Click on the window to see!", "Successful", style = wx.ICON_INFORMATION| wx.OK)
        dlg.ShowModal()
        if dlg.ShowModal == wx.ID_OK:
            dlg.Destroy()
                
    def onGreen(self, evt):
        self.gui.text.SetBackgroundColour(wx.GREEN)
        dlg = wx.MessageDialog(self.gui, "Changed colour!. Click on the window to see!", "Successful", style = wx.ICON_INFORMATION| wx.OK)
        dlg.ShowModal()
        if dlg.ShowModal == wx.ID_OK:
            dlg.Destroy()
        
class Application(wx.App):
    def __init__(self, redirect = False):
        wx.App.__init__(self)
    def OnInit(self):
        frame = GUI(None, -1, "Classes interactions")
        frame.Show(True)
        return True
Application().MainLoop()

StephenMtangoo/Mtangoo Code snippets (last edited 2009-01-01 18:30:24 by vas33)

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