7

When I try to install tkinter using this command:

sudo apt-get install python-tk

I get this message meaning it is already installed:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-tk is already the newest version.
The following package was automatically installed and is no longer required:
  libjpeg62
Use 'apt-get autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.

When I want to import it, I get this message error:

begueradj@begueradj-darwin:~/begueradj# python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tkinter import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tkinter
>>> 

How to fix this ?

4 Answers4

14

If you are running python ver 3.x.x you should install tkinter for python3

sudo apt-get install python3-tk

That worked for me.

3

Note Tkinter has been renamed to tkinter in Python 3. (source:https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter). So in your codes, use import tkinter instead of import Tkinter. Also, in the codes, where there is Tkinter, use tkinter small letter instead.

DrM.ump
  • 31
2

To use Tkinter, all you need to do is to import one module:

import Tkinter

Or, more often:

from Tkinter import *

So just change your import line to import Tkinter for example:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> Tkinter.TkVersion
8.6
>>> 

Source: https://docs.python.org/2/library/tkinter.html#tkinter-modules

1

I am also having some fun with Tkinter recently. It turns out, many Linux distro do not ship the Tkinter package by default. (FWIW, that statement is even true when you run the official Python docker image, despite Tkinter is part of the standard Python library.)

For the sake of completeness, this is how you will install Tkinter manually.

Python 2 Python 3
If your distro's package manager is apt apt install python2-tk apt install python3-tk
If you use Alpine, whose package manager is apk apk add python2-tkinter apk add python3-tkinter
In your python script, you do from Tkinter import * from tkinter import *
RayLuo
  • 151