Wx Python - Code Structure & Event Handling

L

lee.walczak

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()
 
M

Mike Driscoll

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 Style Guide

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
 
S

Stephen D Evans

Lee,

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

http://wiki.wxpython.org/ModelViewPresenter

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
maintain.

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)

Stephen
 
L

lee.walczak

Thanks for the feedback. It is greatly appreciated.
Let me check out your references and see where they take me.
Will post back and let you know how useful this was.

thanks! Lee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top