I wrote the code from tkinter import *
on pycharm and then I tried to open a window by writing the code screen = Tk()
but it showed an error saying "Unresolved reference 'Tk'".
I've tried this in another project before and it used to work; I don't know what's wrong with it now. I also have updated python to the latest version(3.12.2)
from tkinter import *
screen = Tk()
Despite from tkinter import *
being around in a lot of tutorials and documentation, it is not a recommend practice to do so.
Everything that does an import *
, brings a lot of names to the global namespace that are hard for a human to track, and may break some tools. (I guess pycharm would know how to handle it by now (2024), so your problem might be not having tkinter installed at all)
Anyway, first thing:
improt tkinter preserving its namespace. In order not to have to prefix every class, call and constant with tkinter.
, you may do import tkinter as tk
- and then you only need to prefix things with tk.
.
So, that code would become:
import tkinter as tk
screen = tk.Tk()
screen.mainloop()
Now, if pycharm still shows you errors, just try to run the code above: you should get a small anonymous, blank, window somewhere on your screen.
If it does not work, take note of the error. If you are on a Linux system, using the system Python, what takes place is that most distributions break-up the Python language into several packages - so you have to manually install a package named python3-tkinter
(name may vary, according to your Linux distribution). On Windows, that should not happen: the Windows Python installers always install the language in full.
As for Mac - I can't be sure. A Python installed by homebrew should work, but maybe the system Python is missing tkinter. The remedy is installing another Python interpreter and avoid using the system Python for your personal projects.
Also, as @JRiggles had added in the comments in the O.P.: if your file is named "tkinter.py" Python would import that files instead of its own "tkinter" package, and then it would break. Just rename your file to any other name.