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?
You probably need to install it using one of (or something similar to) the following:
sudo apt-get install python3-tk
You can also mention version number like this
sudo apt-get install python3.7-tk
for python 3.7.
sudo dnf install python3-tkinter
Why don't you try this and let me know if it worked:
try:
# for Python2
from Tkinter import * ## notice capitalized T in Tkinter
except ImportError:
# for Python3
from tkinter import * ## notice lowercase 't' in tkinter here
Here is the reference link and here are the docs
Better to check versions as suggested here:
if sys.version_info[0] == 3:
# for Python3
from tkinter import * ## notice lowercase 't' in tkinter here
else:
# for Python2
from Tkinter import * ## notice capitalized T in Tkinter
Or you will get an error ImportError: No module named tkinter
Just to make this answer more generic I borrowed the following from Devendra Bhat's comment:
On Fedora please use either of the following commands
sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
or
sudo dnf install python3-tkinter