Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import tkinter (or Tkinter)

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.

like image 358
Ian Cole Avatar asked Sep 20 '16 17:09

Ian Cole


People also ask

Why Tkinter is not working?

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.

How do you import Tkinter?

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.

Is Tkinter and Tkinter same?

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.

Can I pip install Tkinter?

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.


3 Answers

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
like image 165
Shardul Nalegave Avatar answered Oct 20 '22 18:10

Shardul Nalegave


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.

like image 38
Preston Hager Avatar answered Oct 20 '22 17:10

Preston Hager


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 * 
like image 23
Ron Lauterbach Avatar answered Oct 20 '22 18:10

Ron Lauterbach