Reputation: 741
I'm trying to display a simple window using the python Tkinter module in visual studio community 2015. Whenever I tried I get an error message Here is the code:
from tkinter import *
root = Tk()
theLabel = Labe1(root, text="This is too easy")
theLabel1.pack()
root.mainloop()
Here is the error message:
NameError: name 'Tk' is not defined
How do I solve this problem?
Upvotes: 1
Views: 4688
Reputation: 386210
You will get this result if you have some other module in your pythonpath named "tkinter". For example, if you name your program "tkinter.py", or if a file named "tkinter.py" is somewhere in your path.
The fix is to simply rename your file. When you do "import tkinter", it's importing your file rather than the tkinter module.
An easy way to check what was actually imported is to do this:
import tkinter
print("the imported file is", tkinter.__file__)
Upvotes: 2
Reputation: 43
Looking through some of my other code I noticed one that did the same thing you are trying to do. I modified the your code to match what I had found and I was able to get it to work.
import tkinter as tk
from tkinter import *
root = tk.Tk()
Label(root, text="This is too easy").grid(row=0,column=0)
mainloop()
I have started to use grid instead of pack because it allows more control over the placement of your items. I have noticed that I get the same error sometimes. Importing tkinter itself is the only way I have found to get around it.
Upvotes: 1