Wx Python - Code Structure & Event Handling

lee.walczak at gmail.com lee.walczak at gmail.com
Tue Sep 9 22:05:31 CEST 2008


I have just started writing a GUI using wxpython after finding a
limitation using Tkinter. I have read most tutorials on wxpython and
slowly becoming accustomed considering I started with the latter GUI
tool first!
I must quote first that I am a novice user of python so the issue(s) I
have may seem very obvious but please be patient with me!

I have noticed that all the wxpython references I used for creating my
application(s)  "cram" all the code in the frame subclass. This is
fine when you consider small applications but what about when they
grow into very complex applications? This creates my first question :
Where is it possible to find information on wxpython code practise/
structure when considering complex larger Gui's?

Without any reference I decided to attempt my owm method by breaking
up the top level panels in my frame as individiual class objects. and
then construct the widgets for the panels within the respective
classes. This led to my second problem, how do I use and event in one
Panel to cause an effect in the other Panel ? For example, if I have
button in one Panel and wish to change the text of a label in the
other Panel, what is the best way to do this? Should I break the code
into modules instead?

Of course, you may explain that the way I have approached this is
completely wrong, if so please tell me, I really want to get the basic
structure right before I start making the code more complex.

I look forward to your help


I have listed some code below to help explain what concept I wish to

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None,
        Panel1 = wx.Panel(self, -1,size=(200,200))
        Panel2 = wx.Panel(self, -1,size=(200,200))
        Sizer = wx.FlexGridSizer(2,2,5,5)
        Util1 = Utils1(Panel1)
        Util2 = Utils2(Panel2)

class Utils1():
    def __init__(self, Panel):
        button = wx.Button(Panel,-1, "Button 1")
        Panel.Bind(wx.EVT_BUTTON, self.OnClick, button)
        self.Label = wx.StaticText(Panel,-1, "Handler to me",
        Sizer = wx.BoxSizer(wx.VERTICAL)

    def OnClick(self, Evt):
        self.Label.SetLabel("you changed me")

class Utils2():
    def __init__(self, Panel):
        self.button = wx.Button(Panel,-1, "Button 2")
        Panel.Bind(wx.EVT_BUTTON, self.OnClick, self.button)

    def OnClick(self, Evt):
        """ what is the easiest & accepted Method of changing the text
        a different class instance?"""
        #???.SetLabel("you changed me")

app = wx.PySimpleApp()
frame = Frame()

More information about the Python-list mailing list