For some reason, I can't use the Tkinter
or tkinter
module.
After running the following command in the python shell
import Tkinter
or
import tkinter
I got this error
ModuleNotFoundError: No module named 'Tkinter'
or
ModuleNotFoundError: No module named 'tkinter'
What could be the reason for and how can we solve it?
The Python "ModuleNotFoundError: No module named 'tkinter'" occurs when tkinter is not installed in our Python environment. To solve the error, install the module and import is as import tkinter as tk .
The easiest way to fix this problem is by upgrading your python to use python 3. If upgrading python is not an option, you only need to rename your imports to use Tkinter (uppercase) instead of tkinter (lowercase). Output: As a result, this window proves that your python installation includes the Tkinter module.
The simplest method to install Tkinter in a Windows environment is to download and install either ActivePython 3.8 or 3.7 from here. Alternatively, you can create and activate a Conda environment with Python 3.7 or greater that is integrated with the latest version of Tkinter.
Running python -m tkinter from the command line should open a window demonstrating a simple Tk interface, letting you know that tkinter is properly installed on your system, and also showing what version of Tcl/Tk is installed, so you can read the Tcl/Tk documentation specific to that version.
You probably need to install it using something similar to the following:
For Ubuntu or other distros with Apt:
sudo apt-get install python3-tk
For 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
As you are using Python 3, the module has been renamed to tkinter
, as stated in the documentation:
Note Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
If you're using python 3.9 on Mac, you can simply install tkinter
using brew:
brew install [email protected]
This fixed it for me.
Edit:
As mentioned by others, you can also use the general command to install the latest version:
brew install python-tk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With