seperate the grid method and it will work. Every function in Python needs to return something and if nothing is returned 'None' will set by default. So your variable will become myLabel = None
.
Now that you know why that is bad behavior your should also do it for every other widget in your code.
.
Explaination
To show you what went wrong in your code look at this bit of code here:
import tkinter as tk
root = tk.Tk()
x1 = tk.Label(text='x1')
x1.pack()
print(x1)
root.mainloop()
the Output should be:
.!label
This tells me that x1 is assigned to the label.
Now take a look at this:
import tkinter as tk
root = tk.Tk()
x1 = tk.Label(text='x1')
returned_by_layoutmanager = x1.pack()
print(x1)
print(returned_by_layoutmanager)
root.mainloop()
Your Output will be:
.!label
None
If you may noticed, None
was returned by the layoutmanger.
This is how python works, as soon as somthing is returned by a method/function the interpreter returns to the point he started reading the function/method. It's like the function tells I'm done keep going.
So if you do this:
import tkinter as tk
root = tk.Tk()
x2 = tk.Label(text='x2').pack()
print(x2)
root.mainloop()
Your Output will be:
None
To understand why None
is assigned to x2 and not to .!label
by this line here:
x2 = tk.Label(text='x2').pack()
Try this:
import tkinter as tk
root = tk.Tk()
x1 = tk.Label(text='x1')
x1 = x1.pack()
print(x1)
root.mainloop()
Your Output will be:
None
Its just the same as you do it in your oneliner. First you assign x1 to the instance of Label
class and then you assign x1 to None
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…