0

I am attempting to use Tkinter for the first time on my computer, and I am getting the error in the title, "NameError: name 'Tk' is not defined", citing the "line root = Tk()". I have not been able to get Tkinter to work in any form. I am currently on a macbook pro using python 2.7.5. I have tried re-downloading python multiple times but it is still not working. Anyone have any ideas as to why it isn't working? Any more information needed from me?

Thanks in advance

#!/usr/bin/python

from Tkinter import *
root = Tk()
canvas = Canvas(root, width=300, height=200)
canvas.pack()
canvas.create_rectangle(  0,   0, 150, 150, fill="yellow")
canvas.create_rectangle(100,  50, 250, 100, fill="orange", width=5)
canvas.create_rectangle( 50, 100, 150, 200, fill="green", outline="red", width=3)
canvas.create_rectangle(125,  25, 175, 190, fill="purple", width=0)
root.mainloop()
Bob1122
  • 3
  • 1
  • 2
  • better to add your traceback when you encounter an error – Padraic Cunningham Jul 24 '14 at 00:54
  • Can you explain what you mean by this? – Bob1122 Jul 24 '14 at 00:56
  • If you encounter errors, it is a good idea to add the error traceback when you ask a question, in this case the answer is easy, you have named your .py file the same as the module you are trying to import but normally the full traceback is the best clue to figure out what is wrong. – Padraic Cunningham Jul 24 '14 at 01:00
  • shoot. Dumb mistake naming it Tkinter.py -_- Any way to get Tkinter.py to go back to the actual function I want? – Bob1122 Jul 24 '14 at 01:02
  • just rename your Tkinter.py to something else, maybe something like MyTk.py, always try to avoid using builtin methods etc.. as variable names and using names for your .py files that shadow module names – Padraic Cunningham Jul 24 '14 at 01:06

3 Answers3

3

You have some other module that is taking the name "Tkinter", shadowing the one you actually want. Rename or remove it.

import Tkinter
print Tkinter.__file__
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I entered your program, and it printed out /Users/myuser/Tkinter.py /Users/myuser/Tkinter.py I then typed it again, and it printed out /Users/myuser/Tkinter.pyc /Users/myuser/Tkinter.pyc – Bob1122 Jul 24 '14 at 00:55
  • Now it is returning: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.pyc Making a program with my code in the OP still returns an error, this time by lagging out to the point where I have to manually shut it down, and it prints this: File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1068, in mainloop self.tk.mainloop(n) – Bob1122 Jul 24 '14 at 01:46
  • That's the correct path, so now you have a completely different problem. – Ignacio Vazquez-Abrams Jul 24 '14 at 02:11
  • Is it worth trying to uninstall python and reinstall it? – Bob1122 Jul 24 '14 at 02:15
  • 1
    Uh, no. But it might be worth writing a minimal Tkinter program to see if you can get it to work. – Ignacio Vazquez-Abrams Jul 24 '14 at 02:16
  • Ok. Could the issue be that for the code in the OP, it isn't opening a canvas? Something else online said that the root.mainloop() was meant to keep the program running until the canvas is closed. Since no canvas is opening, it just continues indefinitely. – Bob1122 Jul 24 '14 at 02:20
  • 1
    I don't know, I don't speak Tkinter. But since you have a separate issue, you should open a separate question. – Ignacio Vazquez-Abrams Jul 24 '14 at 02:29
  • Are you sure it's "lagging out"? Did you check to see if a Tkinter window has opened? On some systems it does not automatically start as the frontmost window. – Brionius Jul 24 '14 at 02:29
  • Ty Broinius! It actually opened but was hidden by default. Got it fixed! – Bob1122 Jul 24 '14 at 02:31
1

Please make sure your Python file name is not "tkinter.py", or it will show this error.

XiaoTai
  • 11
  • 1
0

your code is right but the indent is wrong in the import code, instead of using one space use two spaces and try to not type this command:

import tkinter

use this code:

from tkinter import  *
root = Tk()
canvas = Canvas(root, width=300, height=200)
canvas.pack()
canvas.create_rectangle(  0,   0, 150, 150, fill="yellow")
canvas.create_rectangle(100,  50, 250, 100, fill="orange", width=5)
canvas.create_rectangle( 50, 100, 150, 200, fill="green", outline="red", width=3)
canvas.create_rectangle(125,  25, 175, 190, fill="purple", width=0)
root.mainloop()

the problem also could be typing "Tkinter", so type "tkinter" as python in case sensitive, I think this should work, it does for me