The automagic of Tkinter

richard_chamberlain at my-deja.com richard_chamberlain at my-deja.com
Mon Jun 26 10:25:51 EDT 2000


Hi Egbert,

OK I'll let you get away with more than one
question per post, as long as they are not
difficult. ;-)

Your class Knop inherits from Button which in
turn inherits from Widget which inherits from
BaseWidget.

Button has the keyword argument master which
assigns None if no master is passed, which is
what you are doing. Widget passes this straight
to BaseWidget which does this:

if not master:
	if not _default_root:
		_default_root = Tk()
	master = _default_root

So Tkinter is kindly create a Tk() instance in
case you forgot, or were just being stubborn.

Here's mainloop:

def mainloop(n=0):
	_default_root.tk.mainloop(n)

so mainloop is gets called against _default_root
which just happens to be the Tk() instance that
Tkinter created for you.

When you create an a Tk() instance explicitly it
also calls it _default_root except this time you
get a nice convenient reference to it incase you
want to call methods against it.


Because most people are doing from Tkinter import
* you actually get the method mainloop() anyway.
So you needn't type root.mainloop() but just
mainloop() or call against any widget like you
did. It's probably not a good idea to do these
things though, unless no-one is ever going to
have to decipher your code.

and what does Knop mean?

Richard






In article <20000626135336.A8180 at bork>,
  Egbert Bouwman <egbert at bork.demon.nl> wrote:
> Good afternoon,
> The following small script does what I intended
it to do:
> it produces a button, and that button
dissappears when i click on it.
>
>     from Tkinter import *
>     class Knop(Button):
>        def __init__(self,tekst="KNOPJE"):
>            Button.__init__
(self,text=tekst,command=self.quit)
>            self.pack
(side=TOP,fill=BOTH,expand=YES)
>     root=Tk()
>     k=Knop()
>     root.mainloop()
>
> However when I replace the last three lines
with only:
>     Knop().mainloop()
> then the script still works.
>
> This leaves me with a number of questions
(sorry, richard):
> - if 'root=Tk()' is dispensable, then what is
its builtin replacement
>   and what does that replacement do
> - up till now I saw a class object as a
template, that has to be
>   instantiated in order to be of any use.
However here it seems to
>   lead its own life.
> - probably 'mainloop()' plays a crucial role
here, but what are all
>   its functions ?  What is the difference
between
>   'root.mainloop()' and 'Knop().mainloop()'
> I promised Richard to pose only one question
per post, but these
> questions really are one big question about the
Tkinter mechanism.
> egbert
>
> --
> Egbert Bouwman - Keizersgracht 197 II - 1016
DS  Amsterdam - 020 6257991
>
==================================================
======================
>
>



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list