wiseass
wiseass

Reputation: 745

why does tkinter root in Python need to be created first?

I have searched stackoverflow and found various answers, but can't figure out the why. Hoping someone can help or simply point me to somewhere that I could read about the following tkinter issue (self taught, trying to learn tkinter).

I try to follow the approach set forth in "A Simple Hello World Program" at https://docs.python.org/3/library/tkinter.html. And it works and all is good. But I'm experimenting to get to a bare minimum to understand things better and have what I believe to be a chicken and egg conundrum.

This code works just fine to give me a window:

import tkinter as tk
root = tk.Tk()
root.mainloop()

Code noted below works if I put the code above "root =" and does not if below "root =".

var = StringVar()
label = tk.Label(root, textvariable=var, relief=RAISED)
var.set("Hey!? How are you doing?")
label.pack()

I get an error stating "'NoneType' object has no attribute '_root'". I understand it is because root has yet to be defined.

I think Brian Oakley superbly answered my question at AttributeError: 'NoneType' object has no attribute '_root' He wrote: "You cannot create an instance of StringVar until after the root window has been created."

What I'm wondering is why is that the case? I want to understand why as I think it will make me a better programmer. And I've looked and can't put my finger on the why.

Upvotes: 0

Views: 1785

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

why does tkinter root in Python need to be created first?

Tkinter is a wrapper around an embedded tcl/tk interpreter. Every tkinter object represents an object in this embedded interpreter. The interpreter isn't initialized until you create the root window. It is impossible to create a tkinter object which exists in this interpreter until the interpreter exists, so you must create the root window before creating any tkinter objects.

Also, widgets exist in a tree like hierarchy. By definition a tree requires a root. It is literally impossible to create any widget (except the root window itself) without an existing root window. If you create a widget without creating a root window, one will be created for you. That does not happen, however, when you create one of the special Tkinter variables (StringVar, etc).

Upvotes: 2

Related Questions