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.