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 can install Tkinter in any platform (Mac, Linux, Windows) with PIP package manager:
pip install tkintertable
Firstly you should test your python idle to see if you have tkinter:
import tkinter
tkinter._test()
Trying typing it, copy paste doesn't work.
So after 20 hours of trying every way that recommended on those websites figured out that you can't use "tkinter.py" or any other file name that contains "tkinter..etc.py". If you have the same problem, just change the file name.
--------- WORKED ON PYTHON 2.7------------
Install all below packages
sudo apt-get install git
sudo apt-get install python-tk
sudo apt-get install python-pip
sudo apt install picolisp
sudo -H pip2 install --upgrade pip
sudo pip install -I pillow
sudo apt-get install python-imaging-tk
sudo apt-get install python-tk
I think you should try this:
from tkinter import *
or
from Tkinter import *
It really depends on what type of computer you use, or what version of python you have.
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.
For windows 10, it is important to check in the Python install the optional feature "tcl/tk and IDLE". Otherwise you get a ModuleNotFoundError: No module named 'tkinter'. In my case, it was not possible to install tkinter after the Python install with something like "pip install tkinter"
check the python version you have installed by using command python --version
check for the Tk module installed correctly from following code
sudo apt-get install python3-tk
Check if you are using open-source OS then
check the tkinter module in the following path /home/python/site-packages/tkinter change the path accordingly your system
Tkinter should come with the latest Python, I don't think it comes with Python2. I had the same problem but once. I upgraded to Python 3.8 Tkinter was installed.
if it doesnot work in pycharm you can add the module in the project interpreter by searching in +button python-tkinter and download it.
Make sure that when you are running your python code that it is in the python3 context. I had the same issue and all I had to do was input the command as:
sudo python3 REPLACE.py
versus
sudo python REPLACE.py
the latter code is incorrect because tkinter is apparently unnavailable in python1 or python2.
use below.
from tkinter import *
root=Tk()
.....
root.mainloop()
You might need to install for your specific version, I have known cases where this was needed when I was using many versions of python and one version in a virtualenv using for example python 3.7 was not importing tkinter I would have to install it for that version specifically.
For example
sudo apt-get install python3.7-tk
No idea why - but this has occured.
The best way is from tkinter import *
On CentOS7, to get this working with Python2, I had to do:
yum -y install tkinter
Noting this here because I thought that there would be a pip package, but instead, one needs to actually install an rpm.
You should try this :
pip install tkinter
I hope this would solve the issue.
You just need to install it and import them your project like that :
this code import to command line :
sudo apt-get install python3-tk
after import tkinter your project :
from tkinter import *
Check apt for tasks, it may be marked for removed
sudo apt autoremove
Then check and install needed
tkinter comes with python... uninstall python, reinstall it, you're done
For Windows 10 using either VSCode or PyCharm with Python 3.7.4 - make sure Tk is ticked in the install. I tried import tkinter as xyz
with upper/lower t and k's and all variants without luck.
What works is:
import tkinter
import _tkinter
tkinter._test()
An example in action:
import tkinter
import _tkinter
HEIGHT = 700
WIDTH = 800
root = tkinter.Tk()
canvas = tkinter.Canvas(root, height = HEIGHT, width=WIDTH)
canvas.pack()
frame = tkinter.Frame(root, bg='red')
frame.pack()
root.mainloop()
Source: Stackoverflow.com