I was brushing up on Tkinter when I looked upon a minimal example from the NMT Tkinter 8.5 Reference.
#!/usr/bin/env python
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = tk.Button(self, text='Quit',command=self.quit)
self.quitButton.grid()
app = Application()
app.master.title('Sample application')
app.mainloop()
It's all well and good, until I notice that the Tk
class isn't being initialized. In other online reference material I could find (Python's Library Reference, effbot.org, TkDocs), there's usually a call to root = tk.Tk()
, from which the rest of the examples are built upon. I also didn't see any sort of reference to the Tk
class initialization anywhere on the NMT's reference.
The information I could get regarding the Tk
class is also vague, with the Python Reference only listing it as a "toplevel widget ... which usually is the main window of an application". Lastly, if I replace the last lines in the snippet I presented earlier:
root = tk.Tk()
app = Application(root)
The program would run as well as it did before. With all this in mind, what I'm interested in knowing is:
- What does calling
root = tk.Tk()
actually do (as in, what gets initialized) and why can the previous snippet work without it? - Would I run into any pitfalls or limitations if I don't call
Tk()
and just built my application around theFrame
class?