473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Wx Python - Code Structure & Event Handling

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()
Sep 9 '08 #1
3 2720
On Sep 9, 3:05*pm, lee.walc...@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 thetext
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
Sep 9 '08 #2
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
<le*********@gmail.comwrote in message
news:b5**********************************@m73g2000 hsh.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()

Sep 9 '08 #3

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

Sep 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Raymond Hettinger | last post by:
QOTW: "Python seems to encourage and reward incremental effort, and it leads one to explore extensions and improvements to programs because the language makes it relatively easy to see how to do...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
16
by: PyDenis | last post by:
Today, I found strange error while using py2exe: 1. I wrote simple program and save as 1.py: import win32ui import win32con win32ui.MessageBox('Test messageBox.' , 'Test', win32con.MB_OK |...
122
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection...
20
by: Mr.SpOOn | last post by:
Hi, I need a structure to represent a set of integers. I also need to perform on this set some basic set operations, such as adding or removing elements, joining with other sets and checking for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.