Newbie question about Tkinter

A

Andr? Roberge

The following program (Python 2.3, under Windows XP)
===
import Tkinter as Tk
A = Tk.Tk()
A.title("1")
A.mainloop()
B = Tk.Tk()
B.title("2")
B.mainloop()
===
opens window A and waits for it to be closed before opening window B.
However, the following opens both windows "simultaneously". I tought
it would give the same result as the one above... I am confused.
====
import Tkinter as Tk
A = Tk.Tk()
A.title("1")
B = Tk.Tk()
B.title("2")
A.mainloop()
B.mainloop()
====
Anyone can explain or give a pointer to the answer.
André
 
C

Christopher T King

The following program (Python 2.3, under Windows XP)
===
import Tkinter as Tk
A = Tk.Tk()
A.title("1")
A.mainloop()
B = Tk.Tk()
B.title("2")
B.mainloop()
===
opens window A and waits for it to be closed before opening window B.

For the record, you should only use Tk() once in your program; besides
being the main top-level window, it also instantiates Tk and the Tcl
interpreter. If you want multiple top-level windows, use Tk() for the
main one and Toplevel() for any additional ones.
However, the following opens both windows "simultaneously". I tought
it would give the same result as the one above... I am confused.
====
====
Anyone can explain or give a pointer to the answer.

Calling .mainloop() on any widget enters the "main event loop", which
waits for keyboard/mouse events and dispatches them accordingly.
..mainloop() typically doesn't return until the main top-level window (A or
B in this case) is destroyed. In the first example, you're calling
A.mainloop() before creating B; hence A must be destroyed in order for
A.mainloop() to exit. Try each of the above in the interactive
interpreter to see exactly what's going on (Tk works beautifully in an
interactive mode).

Rewriting the above using Toplevel:

import Tkinter as Tk
A = Tk.Tk()
A.title("1")
B = Tk.Toplevel(A)
B.title("2")
A.mainloop()

Passing A as an argument to Toplevel (to specify its master) is optional;
if you don't, Tkinter will automatically pick A as its master.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top