Tkinter import with PyCharm

26,781

Solution 1

from Tkinter import * 

root = Tk()

thislabel = Label(root, text = "This is an string.")

thislabel.pack()

root.mainloop()

Use Tkinter not tkinter

Solution 2

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

Solution 3

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

Solution 4

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

Share:
26,781
PyDer
Author by

PyDer

Eat, sleep, code, repeat.......

Updated on July 09, 2022

Comments

  • PyDer
    PyDer almost 2 years

    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.