You probably need to install it using something similar to the following:
For Ubuntu or other distros with Apt:
sudo apt-get install python3-tkFor Fedora:
sudo dnf install python3-tkinter
You can also mention a Python version number like this:
-
sudo apt-get install python3.7-tk -
sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
Finally, import tkinter (for Python 3) or Tkinter (for Python 2), or choose at runtime based on the version number of the Python interpreter (for compatibility with both):
import sys
if sys.version_info[0] == 3:
import tkinter as tk
else:
import Tkinter as tk
Answer from d-coder on Stack OverflowYou probably need to install it using something similar to the following:
For Ubuntu or other distros with Apt:
sudo apt-get install python3-tkFor Fedora:
sudo dnf install python3-tkinter
You can also mention a Python version number like this:
-
sudo apt-get install python3.7-tk -
sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
Finally, import tkinter (for Python 3) or Tkinter (for Python 2), or choose at runtime based on the version number of the Python interpreter (for compatibility with both):
import sys
if sys.version_info[0] == 3:
import tkinter as tk
else:
import Tkinter as tk
If you're using Python 3.9 on Mac, you can simply install tkinter using Homebrew:
brew install [email protected]
This fixed it for me.
As mentioned by others, you can also use the general command to install the latest version:
brew install python-tk
Just re-install python..
tkinter (and the associated system libraries it needs) are meant to be included by default with any version of python you install. If it got deleted or corrupted (or not installed in the first place), it is easiest often just to re-install python. If you want to keep all the libraries you've already installed, copy c:\Python38\Lib\site-packages somewhere safe, then you can go ahead and delete the python folder. Next you'll want to search using the start menu for "environment variables", and select "edit environment variables for your account". Select the "Path" variable, and click the "edit" button. Delete any entries referring back to the python folder you just deleted.
The recommended windows installer from python.org for 3.8.7 includes several options if you "customize installation" including whether or not to install tkinter as well as where you want to install.
Checking the entry for "add to PATH" will ensure that when you type "python" into a cmd prompt; it works. You can then move your old "site-packages" folder back to your python folder in the same location "pyfolder\Lib\site-packages". If you install a different version of python you should re-install any libraries rather than copying them, but saving site-packages will at least give you a list of what you need to go install.
I also had the same problem (on Windows 10) but with python 3.10.0. PyCharm could not install it for me, it suggested installing Future for some reason. The following solved the problem for me:
(I still had the python installer exe among my Downloads. If you do not have it, download it from here.)
Run the installer, choose Modify (Add or remove individual features), tick tcl/tk and IDLE.
I did not have to edit my PATH or re-install previously installed modules.
No module named 'tkinter' in Pycharm on Windows 10
Tkinter ModuleNotFoundError
python - matplotlib error - no module named tkinter - Stack Overflow
No module named tkinter
Videos
Hi everyone,
Im working in a project using Python and I ran into an issue regarding tkinter. The first time a execute the testing code:
from tkinter import *
from tkinter import ttk
root = Tk()
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
root.mainloop()
Gave me the following error --> ModuleNotFoundError: No module named 'tkinter'
Im using Fedora so I proceed installing tkinter using "sudo dnf install python3-tkinter" command. And when I run "python3 -m tkinter" from terminal it shows the pop-up window. But i keep ran in into the same error when I execute the code from VSC.
Anyone had an idea of how to solve this?
thank you all!
For Linux
Debian based distros:
sudo apt-get install python3-tk
RPM based distros:
sudo yum install python3-tkinter
For windows:
For Windows, I think the problem is you didn't install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html . Good python distributions for Windows can be found by the companies Anaconda or ActiveState.
Test the python module
python -c "import tkinter"
p.s. I suggest installing ipython, which provides powerful shell and necessary packages as well.
you can use
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
if you dont want to use tkinter at all.
Also dont forget to use %matplotlib inline at the top of your notebook if using one.
EDIT: agg is a different backend like tkinter for matplotlib.
Hey guys
I am working in Visual Studio on a school assignment for my Computer Science class. It requires use of the turtle module. I am being told that it comes with python when you install it, however, when I try to use it, I am told that there is no module named ‘tkinter’. Could someone let me know what has to be done to fix this? Thanks
If you are running python ver 3.x.x you should install tkinter for python3
sudo apt-get install python3-tk
That worked for me.
Note Tkinter has been renamed to tkinter in Python 3. (source:https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter). So in your codes, use import tkinter instead of import Tkinter. Also, in the codes, where there is Tkinter, use tkinter small letter instead.
Ok. First off:
What you shouldn't do:
In production, if you are not sure whether a module is called one way or another (which might be depending on the Python version that is installed), you should not put all the imports together like that, because if one fails it will raise an import error and that will crash your runtime. Do the following:
try:
import Tkinter
except ImportError: # Python 3.x present
import tkinter
However in your case you already know that you have Python 3 so that is not a problem. Just use the correct one (keep reading to the next section).
What it is recommendable that you do:
If you are using Python 2.x:
Module is named Tkinter. You can do from Tkinter import * and Tk will be imported.
If you are using Python 3.x:
Module is named tkinter. Note lowercase. You have to do import tkinter; and use tkinter.Tk
Rationale
You might want to read this fragment from this answer already posted on SO:
However, PEP8 has this to say about wildcard imports:
Wildcard imports ( from import * ) should be avoided
In spite of countless tutorials that ignore PEP8, the PEP8-compliant way to import would be something like this:
import tkinter as tkWhen importing in this way, you need to prefix all tkinter commands with tk. (eg: root = tk.Tk(), etc). This will make your code easier to understand at the expense of a tiny bit more typing. Given that both tkinter and ttk are often used together and import classes with the same name, this is a Good Thing. As the Zen of python states: "explicit is better than implicit".
Note: The as tk part is optional, but lets you do a little less typing: tk.Button(...) vs tkinter.Button(...)
Full answer: https://stackoverflow.com/a/11621141/4396006
Why your interpreter does not import Tk
I am uncertain on why your interpreter does not import Tk for that usage. You have to provide more details to be able to solve that part of your problem.
Edit: the line from tkinter import * includes the namespace of the __init__.py file in the tkinter module folder into your file. Therefore you should check:
Where is PyCharm importing the tkinter module from. You can go to the
tkinterword in your import, get the contextual menu with the right click, and go to:Go to --> Declaration(or just hitCtrl+B). It should take you to that__init__.pyfile whereTkshould be a class defined in there.Whether your Python Path when you run the file is fetching that folder you found the Tk module in.
If any of this is not ok, then it's probably because your installation is broken. I'd be helpful if you told us if only from tkinter import * does not work or if import tkinter; tkinter.Tk is not defined either. You should go for a clean install.
To help us know the root of the problem, try to run the same code from the terminal or in the Python's console and see what happens.
Lets clear up some basics as it appears you think several things should work that never will.
No mater how you import you will always need to do Tk() with an upper case T by itself or with the appropriate prefix.
Things you have tried that will never work.
root = tk(), Tk.tk(), root = TK.TK()
all lower case tk() or all uppercase TK() will never work in tkinter.
If from tkinter import * doesn't work and doing top = tkinter.Tk() doesn't work it is very likely that you do not have tkinter installed. Or at least it has been removed for some reason.
Windows distro should come with tkinter already. I would try to do a clean install and see what happens. You should update to 3.6 anyway as 3.5 has some bugs that needed to be fixed.
As for your import issue.
from tkinter import * This line should work fine with top = Tk(). So that tells me tkinter is not installed.
import tkinter This redundant line should work as top = tkinter.Tk() but if the previous is not working then this likely wont either.
After doing some testing on PyCharm I can say that if PyCharm failed to load tkinter then it would have errored out first on the import and not the Tk() portion.
Traceback (most recent call last):
File "C:/Users/mcdomi3/PycharmProjects/MintyFlakes/test.py", line 1, in <module>
from Tkinter import *
ModuleNotFoundError: No module named 'tkinter'
Process finished with exit code 1
With that little revaluation I think your install is corrupt.
Conclusion.
You need to reinstall python or try to pip install tkinter as it is missing from your libraries or corrupt somehow.