1

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.

  1. Tkinter is not installed. I installed both the python-tk and python3-tk packages before updating my packages, saving my file, and running my program with python3 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'
  1. The import name is wrong. Using tkinter with a lowercase gives me the Tk() not defined error, indicating the import name is correct, but the name Tk is not. Using it with an uppercase T gives me a no module named Tkinter error. Using tkinter.Tk() results in a tkinter is not defined error. Installing tkinter on ubuntu 14.04

  2. The case in the code, or the instantiation of Tk(), or similar names, is wrong. Typing

    • root = 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?

Community
  • 1
  • 1
Dylan LaCoursiere
  • 493
  • 1
  • 8
  • 18

3 Answers3

9

You seem to have named the file tkinter.py. You cannot name a file with the module you are importing. Python will try to import from your existing file instead of tkinter module. There will be module name collison. There's no such Tk() defined in your file which generates a NameError.

But, when you renamed the file, the "bad magic number" could be due to the .pyc files(compiled from .py files) which could be causing such errors. Search for and delete those files and rerun. It should resolve the issues.

skshetry
  • 342
  • 1
  • 5
  • 10
  • I renamed the file to mytkinter.py, and deleted the .pyc files and it finally worked! Thanks a lot, though that could have been much less of a pain in the ass than it had to be. – Dylan LaCoursiere Mar 06 '17 at 03:22
  • my file name was gui.py and i still have exact problem as @Dylan – greendino Mar 22 '20 at 02:53
0

Don't name any script with the name of a module, even a partial name. Your clue was here:

File "mytkinter.py"
esponapule
  • 51
  • 6
  • This was already mentioned in my accepted answer: "You cannot name a file with the module you are importing." Can you explain why a partial name would not work? Seems like it'd be more work to search for a string in a filename than a filename. – Dylan LaCoursiere Aug 25 '17 at 00:45
0

if you use root = Tk() in IDLE then it will run but, if you want to use this code in another IDE the you have to use root = tkinter.Tk() may be it will help