'Tk' is not defined [duplicate] - python

This question already has an answer here:
idle-python3.2 not starting: complains of NameError: name 'Tk' is not defined
(1 answer)
Closed 8 years ago.
I am new to Python. I downloaded Spyder 2.3.1 and am running Python 2.7 on my Mac. I tried this sample program:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
When I run, I get the error message:
NameError: name 'Tk' is not defined
If I look in the file Tkinter.py, it has the following lines of code:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
Looks like an infinite loop, but what it is complaining about is "Tk" saying "name not defined". Any help would be greatly appreciated.
p.s. I tried python -m idlelib.idle in a Terminal window and got the error NameError: name 'Tk' is not defined

The filename Tkinter.py prevent the import of the standard library module Tkinter.
Rename the file with different name. Also you shouold remove Tkinter.pyc if there it is.

Related

Instantiating a Canvas in Python : tkinter.TclError: no display name and no $DISPLAY environment variable

Very new to python, just trying to create a Canvas in python using tkinter:
from tkinter import *
top = Tk()
w = Canvas (top , height = 300, width = 300)
Yields error:
_tkinter.TclError: no display name and no $DISPLAY environment variable
Most of what I saw regarding this issue had to do with running code on a remote machine and not having a place to display the output. I was running this on the online compiler:
https://repl.it/repls/MintyHumongousParentheses
Any insight into this error is appreciated, thank you!
The tkinter package is a thin object-oriented layer on top of Tcl/Tk. The problem is Tcl can't find the virtual display.
As stovfl pointed out your problem is the online python editor, I don't know witch one you are using, but repl.it is compatible with remi.gui, see example: https://repl.it/#amasad/tictactoe.
I tested your code locally and it didn't work, so I made a few changes:
from tkinter import *
top = Tk()
w = Canvas(top, width=300, height=300)
w.pack()
top.mainloop()
How do I define root for tkinter in Python 3? I'm using an online Python editor
Tkinter
Python Tkinter not working in a .py file

Tkinter import with PyCharm

I want to create a tkinter window using pycharm:
from tkinter import *
root = Tk()
root.mainloop()
Apparently PyCharm tells me that from tkinter import * is an unused import statement, and root = Tk() is an unresolved reference. What's confusing me is that the code works completely fine, a tkinter window shows up, no errors.
How do I fix this?
Edit: PyCharm shows these error whenever I import any other library I have.
from Tkinter import *
root = Tk()
thislabel = Label(root, text = "This is an string.")
thislabel.pack()
root.mainloop()
Use Tkinter not tkinter
from tkinter import*
works just fine. You just have to go to the next line and type something along the lines of
tk = Tk()
or any tkinter code and it will recognize it and work just fine.
from tkinter import*
tk = Tk()
btn = Button(tk, text="Click Me")
btn.pack()
tk.mainloop()
Does that code above work?
Hope this helps
At my case, the file that I was writing had the name "tkinter.py", when I imported the module "tkinter" what PyCharm did was import the file that I was writing, of course the message error: "Cannot find reference Tk in imported module tkinter" appeared. Its a dumb error, but check that you file not called same as module. ;)
EDIT:
If you use "from tkinter import * " you must run it like this:
from tkinter import *
root = Tk()
root.mainloop()
Note the uppercase "T" in "Tk".
If you use "import tkinter as tk" you must run it like this:
import tkinter as tk
root = tk.Tk()
root.mainloop()
Note the "tk" module (lowercase) before "Tk" (uppercase).
maybe check if you installed python in a virtual environment, if so you need to work your project there too
In the end I managed to fix this problem myself, here's what I did:
Deleted the ".idea" file associated with the project.
In PyCharm: File >> Open >> "path to project" >> Ok (reopen project)
Now it it looks as normal as it was before.
I could solve it by doing the following
Delete the .idea file.
Delete the __py_cache__ file.
in python2 it is
from Tkinter import *
and is python 3 it is
from tkinter import *
I hope this helps somehow.
I have found out!!
You are actually have to install tkintertoy to use tkinter in pycharm.

Python GUI - 2.7 to 3.5

from tkinter import *
#Create the window
root = Tk()
#Modify root window
root.title("Simple GUI")
root.geometry("200x50")
app = frame(root)
label = Label(app, text = "This is a label")
label.grid()
#kick of the event loop
root.mainloop()
I am following a tutorial of YouTube to learn about Python tkinter GUI.
But when I run the above code it comes with an error.
Traceback (most recent call last):
File "C:/Users/Nathan/Desktop/Python/Python GUI/Simple GUI.py", line 14, in <module>
app = frame(root)
NameError: name 'frame' is not defined
I know it is something to do with frame, I tried Frame and it doesn't work.
Can you please help me make it work, Thanks!
I am currently using Python 3.5 and the tutorial is in 2.7
There are two things wrong with your script. The first one gives the error, and you have already worked out how to fix that:
app = Frame(root)
The second problem is that the label won't appear inside the frame without proper layout management. To fix that, call pack() on the frame:
label = Label(app, text = "This is a label")
label.grid()
app.pack()
You did get the fact that the 2.x module is named Tkinter, but in 3.x it is named tkinter. However, the Frame class did not change the first letter to lower case. It is still Frame.
app = Frame(root)
One way to overcome the import difference is in ImportError when importing Tkinter in Python
from tkinter import *
App = Tk()
App.geometry("400x400")
L = Label(App, text="Hello")
L.pack()
You don't need use a frame.
First, understand that whenever you want to create a label or frame make sure you use its first letter capital. For ex. Label() or Frame().
In your above example use:
app = Frame(root)
and then you need to use "grid()" to just nicely pack your frame.
In your above example use:
app.grid()
Best luck!

Visual Studio - NameError: name 'Tk' is not defined

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?
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__)
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.

Why don't I have Tkinter? [duplicate]

This question already has answers here:
ImportError: No module named 'Tkinter'
(27 answers)
Closed 6 years ago.
I'm trying to test GUI code using Python 3.2 with standard library Tkinter but I can't import the library.
This is my test code:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
The shell reports this error:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
from Tkinter import *
ImportError: No module named Tkinter
The root of the problem is that the Tkinter module is named Tkinter (capital "T") in python 2.x, and tkinter (lowercase "t") in python 3.x.
To make your code work in both Python 2 and 3 you can do something like this:
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
However, PEP8 has this to say about wildcard imports:
Wildcard imports ( from <module> import * ) should be avoided
In spite of countless tutorials that ignore PEP8, the PEP8-compliant way to import would be something like this:
import tkinter as tk
When importing in this way, you need to prefix all tkinter commands with tk. (eg: root = tk.Tk(), etc). This will make your code easier to understand at the expense of a tiny bit more typing. Given that both tkinter and ttk are often used together and import classes with the same name, this is a Good Thing. As the Zen of python states: "explicit is better than implicit".
Note: The as tk part is optional, but lets you do a little less typing: tk.Button(...) vs tkinter.Button(...)
The module is called tkinter, not Tkinter, in 3.x.
Rewrite the code as follows with Tkinter as tkinter (lowercase) for 3.x:
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

Categories

Resources