Learning GUI programming & OOP

44 views
Skip to first unread message

Jonno

unread,
Jan 13, 2012, 11:07:23 PM1/13/12
to wxpytho...@googlegroups.com
I'm a noob when it comes to classes and inheritance and I'm looking
for a good example-based tutorial to learn from.

I've written a wxpython GUI application using notebook tabs which is
now full of global statements and I'd like to learn how to properly
use classes and objects to pass data around.

Any suggestions most welcome.

Jonno.

Mike Driscoll

unread,
Jan 13, 2012, 11:21:16 PM1/13/12
to wxpytho...@googlegroups.com
Hi Jonno,
Welcome to the wxPython world and our user list. I would recommend http://zetcode.com/wxpython/ and the wxPython wiki. The wiki isn't very organized, but there's lots of good stuff in it. The zetcode website is pretty good for an overview. I also have a bunch of articles on my blog about wxPython and other Python stuff. Between that stuff and the mailing list, I think you'll find everything you need. There are also two wxPython books that you can acquire that are both good.

-------------------
Mike Driscoll

Blog:   http://blog.pythonlibrary.org

Jonno

unread,
Jan 13, 2012, 11:48:53 PM1/13/12
to wxpytho...@googlegroups.com
Thanks Mike,

I've been using the wxPython wiki quite a bit and in fact it's how I
got started using the notepads.
However I'm still looking for some guidance how to properly handle
data without having to resort to globals.
Here is my example:
In my current code I have 2 notepad tabs/pages which means that all
the buttons, text entries etc are defined in 2 different classes
(wx.Panels). I also define a number of functions within those 2
classes which do things like fetch data and process it.
I need certain data that I'm operating on to be accessible from both
notepad panels. How should I handle this with classes/objects?

> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en

Gadget/Steve

unread,
Jan 14, 2012, 6:15:52 AM1/14/12
to wxpytho...@googlegroups.com
On 14/01/2012 4:48 AM, Jonno wrote:
> Thanks Mike,
>
> I've been using the wxPython wiki quite a bit and in fact it's how I
> got started using the notepads.
> However I'm still looking for some guidance how to properly handle
> data without having to resort to globals.
> Here is my example:
> In my current code I have 2 notepad tabs/pages which means that all
> the buttons, text entries etc are defined in 2 different classes
> (wx.Panels). I also define a number of functions within those 2
> classes which do things like fetch data and process it.
> I need certain data that I'm operating on to be accessible from both
> notepad panels. How should I handle this with classes/objects?
>
>
A good general rule is very like that for good functional programming:
"If you are about to copy and paste more than 2 lines of code then
consider abstracting instead"
So if you have just a couple of basic data fetch methods then start with
a couple of base classes that do things like fetch/set and build up from
there the nice thing with OOP is the possibility of having derived
classes the override some methods.

To answer your last bit there are a couple of ways of doing this but
possibly the best is to have a separate data object that is owned by the
parent level and handles the data initialisation, access, etc., and your
panels can access this via a parent.dataobject references.

Gadget/Steve

Robin Dunn

unread,
Jan 14, 2012, 2:30:19 PM1/14/12
to wxpytho...@googlegroups.com


Google is your friend. Try searching for "OOP tutorial python". This is
one of the results that looks like what you are looking for, a short
simple introduction to the basic concepts:
http://www.voidspace.org.uk/python/articles/OOP.shtml

Here is one where somebody asked a similar question and the answers he
got:
http://stackoverflow.com/questions/3332454/oop-python-oriented-tutorials

This is an online book that I encouraged my boys to read:
http://openbookproject.net/thinkcs/python/english2e/ The chapters on
OOP related things are pretty good.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Jonno

unread,
Jan 14, 2012, 9:23:08 PM1/14/12
to wxpytho...@googlegroups.com
Thanks Robin,

At a brief glance I like the voidspace article. Trust me I've been
googling OOP and Python but for some reason I never found something
simple and clear like that.

--
Sent from my mobile device

Jonno

unread,
Jan 14, 2012, 9:27:18 PM1/14/12
to wxpytho...@googlegroups.com
Steve,

This morning while driving I came to that conclusion. The only thing
I'm not sure about is that I won't know the size of an array when I
create the data object in the parent.

--

Gadget/Steve

unread,
Jan 15, 2012, 4:49:52 AM1/15/12
to wxpytho...@googlegroups.com
On 15/01/2012 2:27 AM, Jonno wrote:
> Steve,
>
> This morning while driving I came to that conclusion. The only thing
> I'm not sure about is that I won't know the size of an array when I
> create the data object in the parent.
>
>
Luckily enough since you are programming in python that is not a
problem, you can either create it as an empty array and append to it
when you do know, create it as an object of type None initially and then
create it as an array when you know the size or assuming it is currently
created by one of the child frames during it's initialisation you can
still create it in the same place but as a member of the parent. If you
do either of the first two then you need to make sure that the methods
that access it can cope with the possibility that they might be called
before the array is populated.

If it is not naturally a resizeable array then you can probably also
move the method that determines the size of the array to the parent as well.

You might also like to take a look at some of the discussions in this
newsgroup, the python newsgroup, and several others about the benefits
of separation of the user interface from the data and methods, while it
seems like an obtuse subject at times it is actually very good practice,
[blasphemy warning] allows for the possibility that you may have to use
a different user interface model, e.g. command line, QT, gtk, etc., at
some time in the future if necessary because of customer or other
requirements [end blasphemy], gives a clear separation between the data,
the processing and the user interface and will often result in a better
design.

Sorry to be preaching this early on a Sunday morning, (thinking about it
what better time), but I have found that the benefits are enormous from
following these principles. To give you an example the current
Python/wxPython based project that I am working on has multiple user
interfaces, (32), as multiple uses of the code but 95% of the code is
common across the applications, there are 296 source files totalling 2.5
Megabytes but less than 60 import wx and several of those only do so to
provide unit test interfaces.

The other nice point that often arises late in a project is when they
say something along the lines of "Very nice GUI but we now need the main
processing to run in a background/batch/scheduled/remote task and just
control it from/get the results via the GUI" if the code and data are
tightly bound together this is a major task if they are not it is
usually (almost) trivial.

Gadget/Steve


Mike Driscoll

unread,
Jan 15, 2012, 6:22:25 PM1/15/12
to wxpytho...@googlegroups.com
Werner and I created the MediaLocker project to show one way to separate out the Model-View-Controller paradigm for wxPython users. We use pubsub a lot too. You can check it out here: http://www.medialocker.pythonlibrary.org/

--

Jonno

unread,
Jan 20, 2012, 4:49:49 PM1/20/12
to wxpytho...@googlegroups.com
Ok so to start simply I want to open a file and assign some data in it to an object. I'm opening the file from the file menu which is defined during __init__ of the main app frame.

Here is my data class definition:

class Data(MyFrame):
   def __init__(self, var1, var2):
       MyFrame.__init__(self)
       self.var1 = var1
       self.var2 = var2


Here is the start of the main frame class definition and the line I'm using to instantiate an instance of the Data class:

class MyFrame(wx.Frame):
   def __init__(self, parent, id, title):
       wx.Frame.__init__(self, parent, id, title)

  def OnOpen(self,event):
      # skip code to open file and assign var1,var2
      self.Data(var1,var2)

     
And here is the line to create the instance of MyFrame:

frame = MyFrame(none, wx.ID_ANY, 'App Name')

I'm getting: MyFrame object has no instance of attribute Data when the line self.Data(var1,var2) is called. What am I doing wrong?

Jonno

unread,
Jan 20, 2012, 10:33:31 PM1/20/12
to wxpytho...@googlegroups.com
Figured it out! Syntax error in my code. 

Jonno

unread,
Jan 20, 2012, 11:15:44 PM1/20/12
to wxpytho...@googlegroups.com
So my next question is:
If I create an object containing a set of data each time I open a file, how do I keep track of these objects? Do I need to assign them a unique name somehow?

Gadget/Steve

unread,
Jan 21, 2012, 2:48:12 AM1/21/12
to wxpytho...@googlegroups.com
That depends if you need to:
  1. Keep and use data from multiple files at once
  2. Keep the data and the source of that data separate

If the answer to the first question is no then you just replace your data each time you read a file, if the first is yes and the second is no then you can just add to the data each time a file is read, e.g. by having a data list if the answer to both questions was yes then you will need to have a data object for each file read/loaded the keeps track of which data is from which file, one way of doing this would be to have a data class that holds the data you are concerned about and the file name that it came from you can then have a list of such objects, another way would be to have a dictionary of data objects with the file name as the key.  If you are dealing with very large amounts of data, (potentially more than would fit in the available memory then consider using a database to store it in and then your methods would consist of read the file, validate the data and store it in the database and methods to access and manipulate the data from that database.  It is also worth taking a look at the Virtual List control, UltimateListCtrl and the Data View Control, (DVC_*), samples in the demo.  The good news is that the python methods for dealing with sets of data, (lists and dictionary's of items or of objects), mean that you do not have to worry too much about unique identifiers.

Hope that is some help.

Gadget/Steve

Jonno

unread,
Jan 21, 2012, 10:14:12 AM1/21/12
to wxpytho...@googlegroups.com
Thanks Steve,

I actually have a couple of sources of data, one is from opening files, the other is transferring data directly from another application.
I do need to keep all the data objects separate.
I will need to limit the total number of data objects to a pretty small number (something like 4-8).

I had played around with both the list idea and dictionary ideas last night but I was struggling a bit with how to keep track of things. Since I only want to have a limited number of objects open at one time perhaps I can have a class variable for the data object class which keeps track of the objects? 

C M

unread,
Jan 21, 2012, 10:15:22 AM1/21/12
to wxpytho...@googlegroups.com
So my next question is:
If I create an object containing a set of data each time I open a file, how do I keep track of these objects? Do I need to assign them a unique name somehow?
 
For use of this list, I'd recommend:

- Trim the email you are responding to, keeping only the relevant parts of the ongoing thread.  This makes things easier to follow, cleaner, etc.

- If you have a new question, post it as a new thread; don't start new questions in an old thread.  Also, give that thread a subject line that is informative and specific, such as, "How to store data in an UltimateListCtrl?" .  This helps with the archive and finding helpful info via Google, etc.

- Lastly, your questions are really Python questions, not wxPython ones (though Gadget/Steve's points about the controls which can contain data does bring it back to wxPython a fair bit).  For Python questions, the Python Google groups list or the Python Tutor List are good.  Questions here are supposed to be about getting widgets to work.  (Though, yes, occasionally things veer into pure Python territory).

Che



Jonno

unread,
Jan 23, 2012, 2:46:16 PM1/23/12
to wxpytho...@googlegroups.com
On Sat, Jan 21, 2012 at 9:15 AM, C M <cmpy...@gmail.com> wrote:

For use of this list, I'd recommend:

- Trim the email you are responding to, keeping only the relevant parts of the ongoing thread.  This makes things easier to follow, cleaner, etc.

- If you have a new question, post it as a new thread; don't start new questions in an old thread.  Also, give that thread a subject line that is informative and specific, such as, "How to store data in an UltimateListCtrl?" .  This helps with the archive and finding helpful info via Google, etc.

- Lastly, your questions are really Python questions, not wxPython ones (though Gadget/Steve's points about the controls which can contain data does bring it back to wxPython a fair bit).  For Python questions, the Python Google groups list or the Python Tutor List are good.  Questions here are supposed to be about getting widgets to work.  (Though, yes, occasionally things veer into pure Python territory).

Che


Apologies Che. I find that queries often drift from one area to another. I appreciate all the help though. 

Jean-Yves F. Barbier

unread,
Jan 23, 2012, 3:34:44 PM1/23/12
to wxpytho...@googlegroups.com
On Sat, 21 Jan 2012 10:15:22 -0500
C M <cmpy...@gmail.com> wrote:

Looks like Jonno wants to make a donation for nice
SCSI HDz ;-)

--
Can I have an IMPULSE ITEM instead?

Jackson, Cameron

unread,
Jan 15, 2012, 7:23:45 PM1/15/12
to wxpytho...@googlegroups.com

I've been able to get my list boxes to use a horizontal scrollbar when the containing window is too small, but once the window gets *really* small (relative to the width of the list box's content), the scrollbar disappears, and the list box expands out to full width, pushing everything else to the right.

 

I do not get this behaviour with wxListCtrl, and at the moment I believe this to be a bug in wxListBox, although obviously it's possible I'm doing something wrong.

 

A longer explanation, along with screenshots and a code sample, can be found here: http://stackoverflow.com/questions/8812957/when-its-containing-panel-or-frame-becomes-too-small-wxlistbox-stops-using-its

 

Any answers you can provide here or on Stack Overflow would be much appreciated.

 

Cheers,

Cam

Cameron Jackson
Engineering Intern
Air Operations

Thales Australia
Thales Australia Centre, WTC Northbank Wharf, Concourse Level,
Siddeley Street, Melbourne, VIC 3005, Australia
Tel: +61 3 8630 4591
cameron...@thalesgroup.com.au | www.thalesgroup.com.au

------------------------------------------------------------------------- DISCLAIMER: This e-mail transmission and any documents, files and previous e-mail messages attached to it are private and confidential. They may contain proprietary or copyright material or information that is subject to legal professional privilege. They are for the use of the intended recipient only. Any unauthorised viewing, use, disclosure, copying, alteration, storage or distribution of, or reliance on, this message is strictly prohibited. No part may be reproduced, adapted or transmitted without the written permission of the owner. If you have received this transmission in error, or are not an authorised recipient, please immediately notify the sender by return email, delete this message and all copies from your e-mail system, and destroy any printed copies. Receipt by anyone other than the intended recipient should not be deemed a waiver of any privilege or protection. Thales Australia does not warrant or represent that this e-mail or any documents, files and previous e-mail messages attached are error or virus free. -------------------------------------------------------------------------

Robin Dunn

unread,
Feb 1, 2012, 1:30:48 PM2/1/12
to wxpytho...@googlegroups.com
On 1/15/12 4:23 PM, Jackson, Cameron wrote:
> I've been able to get my list boxes to use a horizontal scrollbar when
> the containing window is too small, but once the window gets *really*
> small (relative to the width of the list box's content), the scrollbar
> disappears, and the list box expands out to full width, pushing
> everything else to the right.
>
> I do not get this behaviour with wxListCtrl, and at the moment I believe
> this to be a bug in wxListBox, although obviously it's possible I'm
> doing something wrong.
>
> A longer explanation, along with screenshots and a code sample, can be
> found here:
> http://stackoverflow.com/questions/8812957/when-its-containing-panel-or-frame-becomes-too-small-wxlistbox-stops-using-its
>
> Any answers you can provide here or on Stack Overflow would be much
> appreciated.

I suspect it has more to do with how the wx.FlexGridSizer is honoring
the best size of the listctrl's. When it can no longer show the full
size for at least one of them using the proportions specified it is
switching over to giving precedence to the best size for the first one,
or something like that.

BTW, switching it to a horizontal wx.BoxSizer allows your sample to work
the way I think you are expecting it to work.

Reply all
Reply to author
Forward
0 new messages