Wx Python - Code Structure & Event Handling

Stephen D Evans recombinant at hotmail.com
Wed Sep 10 00:16:54 CEST 2008


have you considered using the Model-View-Presenter pattern? There is a nice 
example on the wxPython wiki:


This scales well to complex GUIs. Grasping the concept and writing the 
initial code is the difficult part. Code is then much easier to develop and 

There is also the Model-View-Controller pattern. Discussions about both of 
these patterns can be found on the wxPython wiki and wxPython list archives.

For further simplification/maintainability I would recommend using XRC 
resources to create your widget hierarchies where possible. My preference is 
to use XRCed to generate python code with the resources embedded. Again 
consult the wxPython wiki, wxPython list archives - plus the wxPython DEMO 
(under Window Layout->XMLResource)


<lee.walczak at gmail.com> wrote in message 
news:b502b8cf-ba28-4831-a2b0-647933a85e77 at m73g2000hsh.googlegroups.com...
> Hi,
> 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
> achieve,
> import wx
> class Frame(wx.Frame):
>    def __init__(self):
>        wx.Frame.__init__(self, None,
> title="Application",size=(400,400))
>        Panel1 = wx.Panel(self, -1,size=(200,200))
>        Panel2 = wx.Panel(self, -1,size=(200,200))
>        Sizer = wx.FlexGridSizer(2,2,5,5)
>        Sizer.Add(Panel1)
>        Sizer.Add(Panel2)
>        self.SetSizerAndFit(Sizer)
>        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",
> name="Lab1")
>        Sizer = wx.BoxSizer(wx.VERTICAL)
>        Sizer.Add(button)
>        Sizer.Add(self.Label)
>        Panel.SetSizerAndFit(Sizer)
>    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
> in
>        a different class instance?"""
>        pass
>        #???.SetLabel("you changed me")
> app = wx.PySimpleApp()
> frame = Frame()
> frame.Show()
> app.MainLoop() 

More information about the Python-list mailing list