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.
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
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
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
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.
> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en
--
Sent from my mobile device
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.
> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en
--
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
--
To unsubscribe, send email to wxPython-user...@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
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
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
Looks like Jonno wants to make a donation for nice
SCSI HDz ;-)
--
Can I have an IMPULSE ITEM instead?
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
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.