[Tutor] Re: (probably obvious) question about Tkinter

Abel Daniel abli at freemail.hu
Mon Nov 17 17:38:27 EST 2003


Ben McGinnes  writes:
 [ ... question about closing Toplevel windows ... ]

Well, you could call .destroy() on that Toplevel window.

You could also make each Toplevel window once, (your current code
re-creates them every time one of the functions is called) and use
the .withdraw() and .wm_deiconify() methods to hide and show them.

Some other notes:
You need to call .mainloop() only once. Tkinter programs usually have
the folowing structure:

- make classes (by deriving from Tkinter classes, where appropriate)

- a bit of setup (not much, maybe things like reading config files,
  etc. Mostly only instantiate one of the classes you created above,
  or have simething like 'mainwindow = Tkinter.Tk()' and place some
  widgets in that)

- call .mainloop()

The program won't exit the mainloop until you shut down the gui. As
long as the gui is running, your program is in the mainloop. To do
something actually usefull, you write a function, and hook it up as a
callback. Thats what you did with the 'now' function in your
example. The important thing to note is that when your callback is
called (so in this case, the function 'now'), it is called from the
mainloop. The mainloop is alread running, so you don't have to call
'Tkinter.mainloop()' again.



Most of your program consists of creating widgets like:

    reallyB2 = Tkinter.Button(reallyTL, text="No", command=sys.exit)
    reallyB2.config(bg='blue')
    reallyB2.config(activebackground='purple')

This can be written as:

    reallyB2 = Tkinter.Button(reallyTL, text="No", command=sys.exit
                               bg='blue', activebackground='purple')

Second, I think instead of creating a lot of custom windows, you could
use existing ones instead. Reusing existing code will mean shorter and
better code. The tkSimpleDialog module might provide some useful stuff.

-- 
Abel Daniel



More information about the Tutor mailing list