I am trying to import Tkinter to my project using Python 2.7 and instead I get the error:
ImportError: No module named tkinter
Before anyone says it, I have tried both "Tkinter" and "tkinter" but have gotten the exact same message.
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.
The only difference between Tkinter and tkinter is that Tkinter was initially used with Python 2 and tkinter is used for working with Python 3 or later versions.
Tkinter can be installed using pip. The following command is run in the command prompt to install Tkinter. This command will start downloading and installing packages related to the Tkinter library.
If you are using Ubuntu or Debian OS try this:-
sudo apt-get install python-tk
Or if you are using Python 3:-
sudo apt-get install python3-tk
First try using this code for your imports.
try:
import Tkinter as tk # this is for python2
except:
import tkinter as tk # this is for python3
If this doesn't work, try reinstalling tkinter. If you don't know how to reinstall tkinter look at the tkinter installation page, here.
Some compilers have tkinter preinstalled. For example, if you use IDLE tkinter is preinstalled. As much as I know, if you use IDLE, you have to click a box in order to install tkinter. If you are not using IDLE, check whether tkinter/Tkinter is included in your Site Packages folder. Consider reinstalling the compiler/interpreter you are using. After you made sure it is installed the syntax you have to use depends on the version of Python you are using. I am not quite sure for Python 2, but I think you write:
import Tkinter
For Python 3 you write:
import tkinter
or the more often used:
from tkinter import *
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