473,383 Members | 1,801 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,383 software developers and data experts.

OO design, Python, and GUIs

I'm creating a home-library management program with Python 2.2, PyGTK, and
Glade. While I've been many little programs before, this is my first large
application in Python. My question is how I should organise the OO design
of such a program. There should be, I gather, a core class "MainClass",
which coordinates everything, other specialised classes which report to
MainClass, and the GUI should ideally be in its own class "GuiClass".

Now GuiClass at the moment doesn't know anything about MainClass. When the
program is started, an instance "mainClass" is created, which in turn
creates an instance "guiClass". But how can guiClass report anything to
the mainClass? For example, when the user selects a new file in the GUI,
how can I send the chosen pathname to mainClass?

I imagine that I have to initalise the GuiClass instance in such a way
that it knows about mainClass enough to be able to call its methods, but
I've been unable find out how mainClass could pass itself to the GuiClass
__init__.

Be gentle, I'm somewhat of a newbie. If there's documentation out there on
this, I'll happily take a look.

Christopher Culver
Jul 18 '05 #1
3 1725
Christopher Culver schrieb:
I'm creating a home-library management program with Python 2.2, PyGTK, and
Glade. While I've been many little programs before, this is my first large
application in Python. My question is how I should organise the OO design
of such a program. There should be, I gather, a core class "MainClass",
which coordinates everything, other specialised classes which report to
MainClass, and the GUI should ideally be in its own class "GuiClass".


There is a start up module that will talk to you first, I'd call it
homelib.py. It does the necessary initialisations like loading the
GUI resources, opening the database etc. If you want to encapsulate
this in a class you could call it main or start. I wouldn't give the
feature providing classes generic names like "main". They do something
special, so I would give them names that tell something about their
resonsibilities, e.g. library, libraryItem, book, magazine, storage,
editView, listView etc. Aggregate related classes in a module, related
modules in a package (if you have lots of modules).

The feature modules would be imported by the start up module. It
wouldn't be bad to have a rough plan of your software before you
start to write code. The building blocks (packages, modules) of
your app have "uses" relations. If module a uses b then a has to
import b. A layered approach would probably be helpful:

u | startup, user interface
t |-------------------------------
i | engine (provides the features)
l |-------------------------------
s | storage (file, db, network)

Each layer uses its neighbour below, all layers use utils. The layers
could be distributed across several programs (multi-tiered app) but
that's not necessary, of course.

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------

Jul 18 '05 #2
> I'm creating a home-library management program with Python 2.2, PyGTK, and
Glade. While I've been many little programs before, this is my first large
application in Python. My question is how I should organise the OO design
of such a program.


One solution is to use a layered architeture:

.---------------.
| Presentation |
'------+--------'
|
.---------------.
| Application |
'------+--------'
|
.---------------.
| Persistence |
'---------------'

Each layer would be a python package (or multiple
packages if your app is large). The presentation
layer contains your gui classes. The application
layer contains classes that implement the behavior
of the application. The persistence layer handles
storage of your domain objects. Some people would
add a 'domain layer' between the application and
the persistence layer. I suppose that depends on
the size of your project, but for something small
it's overkill and your domain objects are very
likely generated by an oo-rdb mapper tool anyway.

People have differing opinions over whether or not
your gui classes should directly make use of the
domain classes. Technically, layers in this
architecture should not know about layers other
than the immediate one above or below. Java
people like to create structures to pass data
between layers. I think it's not a very good use
of time and prefer to use the domain objects - the
domain should be one thing not changing that much
(a book's a book, right)?

A decent discussion of this architecture is at:
http://www.therationaledge.com/conte...tegies_pe.html

The best thing to pull from this is to keep the gui
stuff in its own package python package and put
a little effort into keeping the gui out of
your main application code. This will give you
more options in the future - perhaps you'll find
you'd like a web version of the library, or that
FLTK is a sharper looking gui, or you'd like have
a text reader read the list.
Jul 18 '05 #3
On Tue, 18 Nov 2003 05:16:32 GMT, Christopher Culver
<ch******************************@yahoo.com> wrote:
of such a program. There should be, I gather, a core class "MainClass",
which coordinates everything, other specialised classes which report to
MainClass, and the GUI should ideally be in its own class "GuiClass".
That's one solution, it could also be a set of classes - one per
window or "form".
Now GuiClass at the moment doesn't know anything about MainClass. When the
program is started, an instance "mainClass" is created, which in turn
creates an instance "guiClass". But how can guiClass report anything to
the mainClass?
If when you create the guiClass you pass a refernce to mainClass
into the constructor the gui can send messages back to it:

class mainClass:
def __init__(self):
...
self.gui = guiClass(self)
...

class guiClass:
def __init__(self, application):
self.application = application
...
def someCommand(self): # called by a widget somewhere
self.application.someMethod(self.widgetValue)
...

So when the widgets call someCommand it sends the gui
data(widgetValue) back to the mainClass instance which it stores
in its self.application member.

You need to define the protocol between the two classes.
There are some fancier variations on this to improve reuse of
Gui elements, do a search on Model View Controller or MVC on
Google for other ideas.
I imagine that I have to initalise the GuiClass instance in such a way
that it knows about mainClass enough to be able to call its methods, but
I've been unable find out how mainClass could pass itself to the GuiClass
__init__.


The self value in the mainClass methods is a reference to the
mainClass instance. Just pass self to the GUI init.

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
Jul 18 '05 #4

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

Similar topics

9
by: Edilmar | last post by:
Hi, First of all, I'm new in Python... I have worked with manu langs and IDEs, like Delphi, VB, JBuilder, Eclipse, Borland C++, Perl, etc... Then, today I think IDEs like Delphi have a...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
63
by: Davor | last post by:
Is it possible to write purely procedural code in Python, or the OO constructs in both language and supporting libraries have got so embedded that it's impossible to avoid them? Also, is anyone...
3
by: Kenneth McDonald | last post by:
If this is not an appropriate newsgroup for this type of posting, please let me know and (if possible) suggest an alternative. I've done a fair bit of research on the net, but information is...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
12
by: cmk128 | last post by:
Hi I am an os developer, i am developing a gui library. I found that java swing is the most easiest to use gui library , am i correct? I am trying to port swing to c++, is there any thing i...
23
by: gord | last post by:
As a complete novice in the study of Python, I am asking myself where this language is superior or better suited than others. For example, all I see in the tutorials are lots of examples of list...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.