[Tutor] learning Tkinter

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 24 Mar 2002 12:52:19 -0800 (PST)


On Sat, 23 Mar 2002 mikalzet@libero.it wrote:

> Being a complete beginner I would like a tutorial to explain WHY things
> have to be written in a certain way.

When we write Tkinter programs, we often use object oriented programming
concepts; if you haven't played around with classes too much yet, Tkinter
will look somewhat foreign.


> class KeysApp(Frame):
> # all right, I create a new class which inherits from class Frame
> 	def __init__(self):
> # why not just def __init__(): or def __init__(other): ?


'def __init__(other)' will work; it's just an arbitrary convention that we
name the instance 'self'.  Yes, we can use 'other', although it will be a
quite shocking to people.  Sorta like saying "thee" instead of "me".
*grin*


However, 'def __init__()' without giving an argument wouldn't be so
useful: the point of an __init__ function is to initialize an instance ---
we want to define what things our Frame will know about, and to do that,
we probably need to touch it at some point.


> 	self.txtBox=Text(self)
> # I create an instance of Text widget which I call txtBox
> # 'self' again appears twice - I could memorize this and accept it
> # as an article of faith but I would really prefer to understand what
> # it means

Let's break it down into two statements; that'll make it a little easier
to see what's happening:

###
txtBox = Text(self)
###

What this is saying is "create a Text widget, and make sure that it's
connected to myself".  Whenever we attach widgets, we usually have to tell
that widget where it's should connect itself to.  Since we're a Frame, we
want to tell the new Text box that _we_ are that container.

At this point, this Textbox now knows about us... but we want to make sure
that we, the Frame, remember about the Textbox as well.  We want things to
be a two-way street, but unless we do something, we'll drop the local
variable.  That leads us to the next statement:

###
self.txtBox = txtBox
###

which says "create an attribute called txtBox".  Any "attributes" will be
almost like global variables: they'll continue to live even after we exit
out of this '__init__()' function.  One of the reasons for having an
__init__ is to help to construct and initialize our new frame.



> 	self.txtBox.bind("<space>", self.doQuitEvent)
> # bind is a method which takes two parameters ? I suppose documentation on
> # bind will explain this somewhere - but if the example explained it would
> # be easier

Actually, this is somewhat advanced Tkinter programming.  You often don't
need to use bind() at the beginning.  What this is doing is "binding" the
space key to a function called self.doQuitEvent --- that is, when the
space key is pressed, ou doQuitEvent() function will fire off.



> Anybody know of any resource like 'explaining tkinter to elementary school
> children' ?

It's possible to do Tkinter stuff without mixing it too much with OOP
concepts --- I think the main difficulty is that you're chewing too much.
*grin*  How about something like:

###
import Tkinter
root_window = Tkinter.Tk()
label = Tkinter.Label(root_window, text="Hello world")
label.pack()
###

>From what I've seen, the "Introduction to Tkinter" takes a non-OOP
approach to showing Tkinter stuff, and you might find it easier to read:

    http://www.pythonware.com/library/tkinter/introduction/


To understand the example that you were working on before, you'll want dig
a little deeper on the concept of "Object Oriented Programming".  Here's
one place you can look at:

    http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm

Good luck!