PyDer
PyDer

Reputation: 532

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.

Upvotes: 5

Views: 28960

Answers (8)

hahaly
hahaly

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

Tessema
Tessema

Reputation: 7

maybe check if you installed python in a virtual environment, if so you need to work your project there too

Upvotes: 1

Diego Díaz
Diego Díaz

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()
  • 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).

Upvotes: 1

Kahloon818
Kahloon818

Reputation: 1

I have found out!! You are actually have to install tkintertoy to use tkinter in pycharm.

Upvotes: -1

RIPPLR
RIPPLR

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

Dennis Simiyu
Dennis Simiyu

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

A.Eckert
A.Eckert

Reputation: 1

I could solve it by doing the following

  • Delete the .idea file.
  • Delete the __py_cache__ file.

Upvotes: 0

PyDer
PyDer

Reputation: 532

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.

Upvotes: 0

Related Questions