[Tutor] GUIs and Classes

Isaac Hall hall@nhn.ou.edu
Fri, 13 Sep 2002 16:40:34 -0500


As someone who was once a beginner to both GUI's AND class structures at
the same time, I think I may be able to help.
(it should be noted here that in no way do I consider myself a 
programmer
or computer expert, but merely someone who has to use a computer as a 
tool
in my work)
I have found that while you never actually have to use class structures
in creating GUI applications/programs, the real benefit of doing so 
comes
out when you must modify/update any GUI code you have written.

I began writing my GUI programs in python in strict linear fashion.  I 
did this
because I was being forced to learn python, Tkinter, and class 
structures
all at the same time.  I had never written an object oriented program 
in my life,
so needless to say, I was a bit intimidated by doing so.  By writing my 
programs
in the linear fashion (and with some function declarations too) I found 
that I
could make my programs work just fine and dandy, however they 
eventually became
super-large files that were a mess to modify.  So I looked into trying 
to follow
examples in books for putting GUI stuff into classes.  I found that 
suddenly my code
became much clearer and more concise, and to boot, I was no longer 
intimidated by
making object oriented code.

As far as making a frame to hold stuff, that is a bit redundant.  It is 
not always
nessecary, but for instance if you want to put several things in the 
window that
are not of the same widget type, sometimes it is useful in dividing up 
the window
in whatever way you want with frames, and then putting your widgets in 
the frames.
The benefit here is that all size considerations can be made with the 
frames, leaving you
free to not worry about that with all of your other widgets.

I hope this helps a little.  putting GUI objects in classes is not 
particularly
nessecary, but doing so makes it easier on your blood pressure when you
must modify your program after having not looked at it in 6 months.

Ike

On 2002.09.13 16:05 Alan Colburn wrote:
> As a beginner to the world of GUI toolkits, I've noticed that most of
> the
> tutorials I've seen (regardless of the toolkit being taught) follow a
> similar pattern. There's an aspect to the examples that I don't really
> understand. Perhaps someone can clarify for me. I'll use Tkinter for
> this
> message.
> 
> A typical program might look like this (I'm borrowing this from the
> 'Thinking in Tkinter' site):
> 
> from Tkinter import *
> 
> class MyApp:
> 	def __init__(self, myParent):
>      self.myContainer1 = Frame(myParent)
>      self.myContainer1.pack()
>     ##various other widgets are added here
> 
> root = Tk()
> myapp = MyApp(root)
> root.mainloop()
> 
> The part I don't understand relates to the reason the GUI is set up
> within
> its own class. It doesn't have to be. In addition, I'm not quite sure
> of the
> difference between the root and the frame. If I simply bound all the
> widgets
> to the root object the GUI would look the same.
> 
> Thoughts? Thanks, as always -- Al
> 
> p.s. My thanks to those of you who recommended 'Beginning Java
> Objects'  to
> me as a way to better understand OOP. I'm enjoying the book very much.
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>