Is it necessary to call Tk() when writing a GUI app with Tkinter?

John Salerno johnjsal at
Wed Feb 29 23:41:45 EST 2012

> It is not necessarily to call Tk explicitly, which i think is a bug
> BTW. Sure, for simple scripts you can save one line of code but only
> at the expense of explicitness and intuitiveness. Observe
> ## START CODE ##
> import Tkinter as tk
> root = tk.Tk()
> root.title('Explicit Root')
> root.mainloop()
> f = tk.Frame(master=None, width=100, height=100, bg='red')
> f.pack()
> f.mainloop()
> b = tk.Button(master=None, text='Sloppy Coder')
> b.pack()
> b.mainloop()
> ## END CODE ##
> as you can see all three examples work even though the last two don't
> explicitly create a master. The master is still there however Tkinter
> just created "magically" for you. Talk about laziness!

I'm not sure I understand which method you are advocating. It sounded like you said calling Tk() explicitly is a bug. I certainly would never create widgets without first creating a master frame, but is creating a Frame object enough, or should I create a Tk object and *then* a Frame object?

Also, at what point do you include the destroy method in your program, assuming you do not have a widget that will close the window? If you only want the Windows "X" button to close the window, then is it okay to leave out any call to destroy()? Or should always explicitly destroy it just as I explicitly created a Tk instance? If the latter, then where in the code do you put the call to destroy so it won't conflict with the user closing the window with the X button?

More information about the Python-list mailing list