[Tutor] gui coding
waynejwerner at gmail.com
Mon Dec 13 19:30:01 CET 2010
On Mon, Dec 13, 2010 at 11:51 AM, Rance Hall <ranceh at gmail.com> wrote:
> When I learned FORTRAN years ago they didn't teach us OOP or what I
> like to call Class based programming.
That must have been a few years ago, then ;)
> since then I've sort of always fallen back to be a procedural
> programmer with lots of functions.
There's nothing really wrong with a lot of functions, and that also
highlights one of the great advantages of Python - you can still program
procedurally or using any other type of paradigm, more or less.
> Python and the tkinter (Tkinter on Versions < 3) seem like a great way
> to write cross platform GUI apps for small light duty apps.
Perfect, really. You can quickly throw a nice little GUI together with few
problems. And there are other people doing some rather
> I've tried to wrap my head around class based programming before and
> it didn't take. But it appears I'm going to have to try again as I
> can not find any tkinter samples that use a procedural approach. I'm
> finding it very difficult to understand what I need to get from
> tkinter because the classes are getting in the way and I'm not seeing
> what I need to see.
> Is there something about class based programming that GUI apps prefer
> to work better in?
> Does anyone have or know of a good tutorial or explanation of class
> based coding that I could have a run at?
I'm not aware of any specific tutorials, but I'll try to answer your
question and give a bit of an explanation.
First, if you're used to writing plenty of functions, you're about halfway
to OOP. The only real difference between what you do already (using
functions) and using classes is on a conceptual level. Classes can be
defined as a collection of data and functions that operate on that data.
Think back to a program that you've written with several related functions.
Perhaps you wrote something with several records that you may have stored in
a list or array or some such. So you may write a function called
get_new_record() that gets data for a record, either from the user or
somewhere else. Then you might have an update_record() function that
modifies a given record. And perhaps even a delete_record(). You could think
for a few minutes and come up with the rudimentary functions, I'm sure.
Now, instead of thinking about them as either lists inside a list or a
series of data elements stored in a list, imagine those records really are
"things" - objects that can stand alone. And you can tell that record that
you want to modify it, or you want it displayed some certain way, or any
number of things. The hardest part about OOP is deciding how much
responsibility an object really should have. Anyhow, that's all OOP is -
just taking the related functions that you would normally write and sticking
them inside a class. So instead of:
['somename', 'somedata', 'anothername', 'more data', 'no name', '']
you could have
[record1, record2, record3]
which you could modify the __repr__/__string__ methods to print out however
If you haven't made the link yet, GUIs tend to be objects because it's just
easier to think of them that way. If you have a (real life) bottle, you can
open the lid, close the lid, put stuff in, take it out, or throw it in the
garbage. It's a lot easier to think of a text entry box as something you can
put text in, or get text out of, or <insert analogy here>.
It's a different way of thinking about programming that begins feeling
entirely natural because we, as humans, are used to talking about things
that can do stuff and we can do stuff with. You might have hedge clippers
that have certain attributes - number and sharpness of blades, for instance.
They also have a function that you can .open() them and .close() them. But
if you close them with something inside they will .cut() the object you
place inside the clippers.
Alan Gauld (frequent contributor of this list) has a tutorial on OOP at
http://alan-g.me.uk/ that has some pretty solid examples about class-based
Of course, this discussion wouldn't be complete without telling you that it
is quite possible (using Tkinter especially) to actually adhere to
procedural programming. You could do something like this:
import Tkinter as tk
print "Yay, you clicked me!"
root = tk.Tk()
button = tk.Button(root, text='Click me!', command=buttonclicked)
If you're more comfortable with procedural programming you can certainly do
it that way, but you'll probably find it a lot easier to spend some time
getting used to the concept of OOP and writing your GUI programs in a
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor