For some reason, I can’t use the Tkinter
or tkinter
module.
After running the following command in the python shell
JavaScript
x
2
1
import Tkinter
2
or
JavaScript
1
2
1
import tkinter
2
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?
Advertisement
Answer
You probably need to install it using something similar to the following:
For Ubuntu or other distros with Apt:
JavaScript121sudo apt-get install python3-tk
2
For Fedora:
JavaScript121sudo dnf install python3-tkinter
2
You can also mention a Python version number like this:
-
JavaScript121
sudo apt-get install python3.7-tk
2
-
JavaScript121
sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
2
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):
JavaScript
1
6
1
import sys
2
if sys.version_info[0] == 3:
3
import tkinter as tk
4
else:
5
import Tkinter as tk
6