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.
Related
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
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.
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.
My problem is that my python code is not working when I run it as a .py file. Here is the code:
import tkinter
tk=tkinter.Tk()
canvas=tkinter.Canvas(tk, width=500, height=500)
canvas.pack()
There is more code to it than that, but that is the relevant stuff. It works fine when I use the python shell or type it directly into the python console, but when I run it as a .py file, it seems to skip this code and go on to the rest, without displaying a canvas. I am using windows, but I am not sure what version of python I'm using.
I was also using
from * import tkinter
before, with relevant changes to the code and i changed it to try and help fix it. It didn't work :(
You are missing the eventloop at the end:
import tkinter
tk=tkinter.Tk()
canvas=tkinter.Canvas(tk, width=500, height=500)
canvas.pack()
# Enter into eventloop <- this will keep
# running your application, until you exit
tk.mainloop()
Only a personal recommendation: don't use tk as a variable name, use app or root or even win/window
The following is a function I created, and put it in a file called last_function.py
from tkinter import*
def new_gui(app,sound_file,mixer):
track=mixer.Sound(sound_file)
def track_toggle():
if ballCheckbutton.get()==1:
track.play(loops=-1)
else:
track.stop()
ballCheckbutton=IntVar()
c1=Checkbutton(app,text="check me out",command=track_toggle,variable=ballCheckbutton)
c1.pack(side=LEFT)
ballScale=DoubleVar()
def ScaleVolume(v):
track.set_volume(ballScale.get())
ballScale.set(track.get_volume())
s1=Scale(app,variable=ballScale,resolution=0.1,command=ScaleVolume,orient=HORIZONTAL,from_=0.0,to=1.0,label="volume")
s1.pack()
and this is the file i use.. to call the code and run it..
from tkinter import *
import pygame.mixer
from last_function import*
app=Tk()
mixer=pygame.mixer
mixer.init()
new_gui(app,"49119_M_RED_HardBouncer.wav",mixer)
def close():
mixer.stop()
app.destroy()
app.protocol("WM_DELETE_WINDOW",close)
app.mainloop()
Everything works fine.. but my query is...
1> Why can't I remove from tkinter import* from the last_function file.. cause anyway it's got that on the top of the file that's calling it right. Why do I get an error saying IntVar() not defined.
2> Why do I have to pass mixer as a parameter in the function? can the function not inherit it directly from import pygame.mixerthat's on top of the file calling it?
What I mean to say is. THERE ARE TKINTER COMPONENTS ALSO BEING USED, BUT I DON'T PASS TKINTER AS A PARAMETER.. Do I ! then why is there this... selective parameter assignment??
I'm really confused!!!
1> Why can't i remove from tkinter
import* from the last_function file..
cause anyway it's got that on the top
of the file that's calling it
right.Why do i get an error saying
IntVar() not defined
The Python "import" follows the same scoping rules as the rest of the Python language. By "import" at the top of your second files does not make the Tkinter namespace available to the last_function.py module. Tkinter also needs to be imported there.
2>why do i have to pass mixer as a
parameter in the function? can the
function not inherit it directly from
import pygame.mixerthat's on top of
the file calling it? WHAT I MEAN TO
SAY IS. THERE ARE TKINTER COMPONENTS
ALSO BEING USED,BUT I DON'T PASS
TKINTER AS A PARAMETER.. DO I!! then
why is there this.. selective
parameter assignment??
With the way you have this coded, you need to pass mixer because you are modifying it in your second file with:
mixer.init()
If you reimported mixer in your last_function.py, you would be getting another instance of mixer and not the one previously imported. There is nothing selective about this since both of your files have the Tkinter namespace imported.
You should try and re-factor this code to avoid having to import Tkinter into two modules and having to init mixer in one module and pass it to another.