Reputation: 532
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.
Upvotes: 5
Views: 28960
Reputation: 33
in python2 it is
from Tkinter import *
and is python 3 it is
from tkinter import *
I hope this helps somehow.
Upvotes: 0
Reputation: 7
maybe check if you installed python in a virtual environment, if so you need to work your project there too
Upvotes: 1
Reputation: 11
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()
If you use "import tkinter as tk" you must run it like this:
import tkinter as tk
root = tk.Tk()
root.mainloop()
Upvotes: 1
Reputation: 1
I have found out!! You are actually have to install tkintertoy to use tkinter in pycharm.
Upvotes: -1
Reputation: 316
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
Upvotes: 1
Reputation: 51
from Tkinter import *
root = Tk()
thislabel = Label(root, text = "This is an string.")
thislabel.pack()
root.mainloop()
Use Tkinter
not tkinter
Upvotes: 4
Reputation: 1
I could solve it by doing the following
__py_cache__
file.Upvotes: 0
Reputation: 532
In the end I managed to fix this problem myself, here's what I did:
Now it it looks as normal as it was before.
Upvotes: 0