Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between tkinter's Tk and Toplevel classes?

In Python 3, I run the following from the interactive shell:

>>> import tkinter
>>> type(tkinter.Tk())
<class 'tkinter.Tk'>
>>> type(tkinter.Toplevel())
<class 'tkinter.Toplevel'>

Both of these create individual windows. I assume that tkinter.Tk() returns the "main" window of the tkinter app, while any additional windows should be created with tkinter.Toplevel().

I noted that if you close tkinter.Tk()'s window, both windows close. Also, if you call tkinter.Toplevel() without a call to tkinter.Tk(), two windows are created (one of them being the "main" window that, when closed, will also close the Toplevel window).

Is this accurate? Are there any other differences that I should be concerned with?

like image 972
Al Sweigart Avatar asked Apr 15 '15 16:04

Al Sweigart


1 Answers

Your summary is accurate. One of tkinter's core architectural features is that the widgets exist in a hierarchy with exactly one root window. That root window is what you get when you instantiate Tk.

Instantiating Tk does more than create a window, it initializes the entire tkinter framework. It actually starts up a hidden tcl interpreter which does the actual work of managing widgets. Tkinter is just a python wrapper around this interpreter.

If you attempt to create some other widget without explicitly creating a root window first, one will be created automatically since every tkinter application must have exactly one root window.

like image 142
Bryan Oakley Avatar answered Oct 24 '22 01:10

Bryan Oakley