'Tkinter name error: "tk" Name is not defined
I am learning how to use Tkinter and I am receiving the following error:
Traceback (most recent call last):
File "/Users/hetparikh/PycharmProjects/BudCalculator/main.py", line 1, in <module>
from tkinter import *
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 3, in <module>
root = Tk()
NameError: name 'Tk' is not defined
from tkinter import *
root = Tk()
theLabel = Label(root, text="This is too easy!")
theLabel.pack()
root.mainloop()
Solution 1:[1]
Try instead:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="This is too easy!")
label.pack()
root.mainloop()
Solution 2:[2]
import tkinter as tk
root = tk.Tk()
theLabel = tk.Label(root, text="This is too easy!")
theLabel.pack()
root.mainloop()
this should work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TheOneMusic |
| Solution 2 |
