[Tutor] Why does "window = tk.Tk()" open a tkinter window when typed in the Python interpreter?

boB Stepp robertvstepp at gmail.com
Thu Aug 27 21:34:45 EDT 2020


I learned something new today about tkinter from a book [1], but I do
not understand why it happens.  If I do this in the Python
interpreter:

Python 3.8.3 (default, Jul 11 2020, 16:27:50)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
3.8.3:  import tkinter as tk
3.8.3:  window = tk.Tk()

A tkinter window opens despite there being no explicit call to
window.mainloop().  Why is this so?

And even more interesting (and useful) I can see the execution of a
tkinter window being built widget by widget like so with the Label
widget:

3.8.3:  label = tk.Label(window, text="First label.")
3.8.3:  label.pack()
3.8.3:  label.config(text='Second label.')

I am greatly looking forward to someone's explanation of why all of
this works without an explicit call to window.mainloop().

[1] "Practical Programming -- An Introduction to Computer Science
Using Python 3.6, Third Edition" by Paul Gries, et al, c. 2017.
Chapter 16. Creating Graphical User Interfaces, p. 324.

-- 
boB


More information about the Tutor mailing list