I need help with making this simple Tkinter program work on Ubuntu 16.04.1 with Python 3.5.2.
Here's the code:
from tkinter import *
root = Tk()
mylabel = Label(root, text="Test")
mylabel.pack()
root.mainloop()
The code as-is gives me this error:
Traceback (most recent call last):
File "tkinter.py", line 8, in <module>
from tkinter import *
File "/home/dylan/Documents/proj/python/tkinter.py", line 10, in <module>
root = Tk()
NameError: name 'Tk' is not defined
I noticed browsing other questions that there are a few popular things that can go wrong when making a Tkinter program.
- Tkinter is not installed.
I installed both the
python-tk
andpython3-tk
packages before updating my packages, saving my file, and running my program withpython3 tkinter.py
.
Note: renaming my file to something other than tkinter.py
results in a weird error:
Traceback (most recent call last):
File "mytkinter.py", line 8, in <module>
from tkinter import *
ImportError: bad magic number in 'tkinter': b'\x03\xf3\r\n'
The import name is wrong. Using
tkinter
with a lowercase gives me theTk() not defined
error, indicating the import name is correct, but the name Tk is not. Using it with an uppercase T gives me ano module named Tkinter
error. Usingtkinter.Tk()
results in atkinter is not defined
error. Installing tkinter on ubuntu 14.04The case in the code, or the instantiation of
Tk()
, or similar names, is wrong. Typingroot = tk()
root = Tk()
root = tkinter()
root = Tkinter()
root = tkinter.tk()
root = tkinter.Tk()
root = Tkinter.tk()
root = Tkinter.Tk()
root = tk.Tk()
root = Tk.Tk()
all result in NameErrors. Programming in Python: Getting "name 'Tk' is not defined" only at Command Prompt, works in IDLE
Another thing to note: the command python3 -m idlelib.idle
, as seen in the question below, results in a Tk not defined
error as well.
'Tk' is not defined
What could be the problem here?