Wx Python - Code Structure & Event Handling

Mike Driscoll kyosohma at gmail.com
Tue Sep 9 23:20:03 CEST 2008


On Sep 9, 3:05 pm, lee.walc... at gmail.com wrote:
> 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()

These are good questions for the wxPython list. You'll learn a lot
there: http://wxpython.org/maillist.php

You'll also find the Style Guide helpful: http://wiki.wxpython.org/wxPython%20Style%20Guide

In my more complex applications, I'll do the widgets in their own
function, or for a wx.Notebook, I'll do each "book" in their own
module by subclassing a wx.Panel object or some such. If you do that
sort of thing in their own module subclasses, then the event handlers
can go there too.

Mike



More information about the Python-list mailing list