344

I am trying to import Tkinter. However, I get an error stating that Tkinter has not been installed:

ImportError: No module named _tkinter, please install the python-tk package

I could probably install it using synaptic manager (can I?), however, I would have to install it on every machine I program on. Would it be possible to add the Tkinter library into my workspace and reference it from there?

Karan
  • 14,824
  • 24
  • 91
  • 157
  • did the solution the answer suggested work for u? It did not for me: `E: Unable to locate package python-tk` did anyomne have this issue? – Charlie Parker Oct 06 '17 at 19:51
  • 8
    For what it's worth, ***if you get this error with*** `matplotlib` it is not always necessary to install Tkinter. Call `matplotlib.use('Agg')` right after importing `matplotlib`. – Daniel F Jan 24 '18 at 19:36
  • 8
    You can use `import matplotlib` `matplotlib.use('agg')` `import matplotlib.pyplot as plt` if you dont wish to install `tkinter` – markroxor Apr 23 '18 at 19:45
  • 9
    If you use `python3.6` do `sudo apt-get install python3.6-tk` –  Jun 09 '18 at 16:06
  • I am using python 3.8 and I still cannot import Tkinter: Tried sudo apt-get install python-tk and sudo apt-get install python3.8-tk but not able to see it in pycharm (using a virtual environment) Reading package lists... Done Building dependency tree Reading state information... Done Note, selecting 'python3-tk' instead of 'python3.8-tk' python3-tk is already the newest version (3.6.9-1~18.04). 0 upgraded, 0 newly installed, 0 to remove and 19 not upgraded. – Shravya Boggarapu Sep 14 '20 at 06:46

25 Answers25

463

It is not very easy to install Tkinter locally to use with system-provided Python. You may build it from sources, but this is usually not the best idea with a binary package-based distro you're apparently running.

It's safer to apt-get install python-tk on your machine(s). (Works on Debian-derived distributions like for Ubuntu; refer to your package manager and package list on other distributions.)

9000
  • 39,899
  • 9
  • 66
  • 104
  • 2
    @BryanOakley: Certainly. The poster seemed to have an apt-based Linux box, thus the answer. This looked like a narrow problem. I didn't expect this answer to get so many upvotes. – 9000 Dec 11 '14 at 14:51
  • 88
    And for python3 `apt-get install python3-tk` at the time of this comment for ubuntu 15.04 – jmunsch Apr 19 '15 at 18:08
  • 3
    I also did it for python3.4 and ubuntu 15.04 : `apt-get install python3-tk` – Hassan Ketabi Jul 24 '16 at 11:40
  • 5
    Just did this exercise on Mint18 (derived from 16.04 Ubuntu) and it was doubleplusgood. `sudo apt install -y python3-tk` – reverend Aug 18 '16 at 22:39
  • 1
    How does one install tkinter from sources? I have tcl/tk, verified using `wish`. I can't find the source tarball for tkinter. – lapin Mar 06 '17 at 18:19
  • @lapin: I'm afraid you need to build from the whole CPython source tree, since [tkinter seems to depend on a module from that tree](https://github.com/python/cpython/blob/master/Modules/_tkinter.c). – 9000 Mar 06 '17 at 19:37
  • `apt-get install python3-tk` confirmed to work for Python 3.5 with Ubuntu 16.04. – rayryeng Mar 12 '17 at 01:37
  • I would like to add that on Fedora the package is python3-tkinter, there is also a python2 version available. – mikeymop Sep 14 '17 at 23:10
  • `E: Unable to locate package python-tk` did anyomne have this issue? – Charlie Parker Oct 06 '17 at 19:50
  • This doesn't work on Debian Jessie 8. there is no code in that package. Just checked using "dpkg -L python-tk" – Eamonn Kenny Nov 22 '17 at 12:05
  • All you had to do was come in as an Administrator using `sudo su` – Precious Tom Jun 05 '18 at 09:56
  • For python 3.7 I had to use `sudo apt-get install python3.7-tk` to make it work – ilias Aug 21 '19 at 05:47
  • Indeed there must be a way to side-load the `Tcl/Tk` DLL's and use `tkinter` as an alias. I face a similar problem and have managed to use anything **but** *Tk* with python as reported [here](https://stackoverflow.com/a/65367570/1147688). – not2qubit Dec 19 '20 at 07:44
121

Actually, you just need to use the following to install the tkinter for python3:

sudo apt-get install python3-tk

In addition, for Fedora users, use the following command:

sudo dnf install python3-tkinter
Neil
  • 1,219
  • 1
  • 8
  • 2
  • 5
    Thanks! It actually requires python3-tk, not python-tk. – zkan Apr 19 '13 at 10:26
  • If you're a Linux newbie like me, note that if apt-get has problems finding / installing python3-tk, try running `sudo apt-get update` first. In my 32-bit Linux Mint 18.3 system, Synaptic and apt-get seem to require that more often than I would have supposed... – RBV Feb 20 '18 at 00:38
  • I get python3-tk is already the newest version (3.5.1-1) and when i import tkinter I still get: ModuleNotFoundError: No module named 'tkinter' I Use(Python 3.7.5) – sqp_125 Dec 09 '19 at 18:30
  • 1
    Solution: sudo apt-get install python3.7-tk – sqp_125 Dec 09 '19 at 18:32
75

If, like me, you don't have root privileges on your network because of your wonderful friends in I.S., and you are working in a local install you may have some problems with the above approaches.

I spent ages on Google - but in the end, it's easy.

Download the tcl and tk from http://www.tcl.tk/software/tcltk/download.html and install them locally too.

To install locally on Linux (I did it to my home directory), extract the .tar.gz files for tcl and tk. Then open up the readme files inside the ./unix directory. I ran

cd ~/tcl8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711
make
make install

cd ~/tk8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711 --with-tcl=/home/cnel711/tcl8.5.11/unix
make
make install

It may seem a pain, but the files are tiny and installation is very fast.

Then re-run python setup.py build and python setup.py install in your python installation directory - and it should work. It worked for me - and I can now import Tkinter etc to my heart's content - yipidy-yay. An entire afternoon spent on this - hope this note saves others from the pain.

Acsor
  • 1,011
  • 2
  • 13
  • 26
Simon
  • 759
  • 5
  • 2
  • 2
    Hi Simon. I just get off the boad of linux. I need to install tcl/tk and python locally. I want to install them to "/home/cnel711/install/". After Installing the tcl/tk. should I set the path for Python? and how. I also use ./configure to install python. thanks – Samuel Aug 14 '12 at 07:23
  • Hi I already have tcl/tk installed locally and I rebuilt python. But, while building python(locally), I am getting an error: libtk8.6.so: cannot open shared object file: No such file or directory, and that the module _tkinter has failed to be built. I even tried setting the path to my tcl/tk libraries with `--with_tcltk_libs` but this didn't help. – rivendell Sep 23 '14 at 23:54
  • Thanks this helped me a lot! Very tiny addition: "If --exec-prefix is not supplied, it defaults to --prefix." from python docs: https://docs.python.org/2/install/ – Tom Dec 11 '14 at 11:35
  • 7
    What setup.py file are you referring to? Where can I find that one? I'm lost there. – David Merinos Jun 30 '15 at 02:04
  • I can't find a setup.py and if he refers to the python's own file, since he assumes we have no privileges we can't do this for that python. A virtualenv doesn't have a setup.py file – JamesTR Aug 24 '16 at 19:39
  • 2
    @DavidMerinos, I suppose the `setup.py` file is to be found in the source directory where you originally installed Python - it is where I found it. If you didn't delete it but forgot where this directory is, you can run a `find ~ -type f -name setup.py` command from anywhere your shell. – Acsor Jun 25 '17 at 14:11
  • 5
    I have over 100 files named `setup.py` on my machine. Which one are you talking about? – jodag Sep 02 '17 at 21:23
  • Have to install X11. `sudo apt install libx11-dev` – Chris P May 22 '20 at 10:36
  • This only installs the Tcl/Tk C libraries themselves (which might already be present). It does not install Python's C bindings to those libraries (`_tkinter`), as described in the OP. `setup.py` normally refers to a Python script provided with a third-party Python package - nothing to do with rebuilding Python itself. Aside from that, recompiling the *system* Python sounds... scary at best. It seems that "local install" here is meant to convey "separate Python built from source". In which case, rebuilding should instead look like another invocation of `make` and/or `configure`. – Karl Knechtel Apr 26 '23 at 19:13
  • When installing tk I had to use `sudo ./configure --prefix=/home/mcadev --exec-prefix=/home/mcadev --with-tclConfig=/home/mcadev/tcl8.5.11/unix --with-tkConfig=/home/mcadev/tk8.6.13/unix`, note the difference of --with-tclConfig and --with-tkConfig – LukeDev Jul 10 '23 at 15:25
49

If you are using Python 3 it might be because you are typing Tkinter not tkinter

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
user1497423
  • 707
  • 7
  • 3
36

For Arch Linux users, it goes a bit like

sudo pacman -S tk
yermama
  • 369
  • 3
  • 2
27

you will need the package and its dependencies.

since you mentioned synaptic, you must be using a Debian based system. one way to get what you need:

sudo apt-get install python-tk
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
20

For Python 2.7:

As it says here,

You don't need to download Tkinter - it's an integral part of all Python distributions (except binary distributions for platforms that don't support Tcl/Tk).

In my case, on Windows, what helped was reinstalling the Python distribution. A long time ago, I had unchecked the "Tcl/Tk" installation feature. After reinstalling, all works fine and I can import _tkinter and import Tkinter.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
14

If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module

yum install tkinter
Jonathan L
  • 9,552
  • 4
  • 49
  • 38
9

For python 3.7 on ubuntu I had to use sudo apt-get install python3.7-tk to make it work

ilias
  • 1,345
  • 1
  • 9
  • 16
8

for python3 user, install python3-tk package by following command sudo apt-get install python3-tk

M. Balcilar
  • 518
  • 5
  • 8
7

tk-devel also needs to be installed in my case

yum install -y tkinter tk-devel

install these and rebuild python

JasonWayne
  • 1,724
  • 1
  • 19
  • 16
5

Fedora release 25 (Twenty Five)

dnf install python3-tkinter

This worked for me.

NIK
  • 1,089
  • 13
  • 22
4

If you're using Python 3 then you must install as follows:

sudo apt-get update
sudo apt-get install python3-tk

Tkinter for Python 2 (python-tk) is different from Python 3's (python3-tk).

gon1332
  • 1,930
  • 1
  • 24
  • 30
4

There is _tkinter and Tkinter - both work on Py 3.x But to be safe- Download Loopy and change your python root directory(if you're using an IDE like PyCharms) to Loopy's installation directory. You'll get this library and many more.

4

Tkinter is a GUI module for python. you can use it to make GUI based applications in python. Tkinter provides several GUI widgets like buttons,menu, canvas,text,frame,label etc. to develop desktop applications.Though Tkinter is very popular and is included with windows, macosx install of python, There are also alternative choices like pyQt, wxPython...

In this tutorial we will see how to install it on linux and use it with an example.

First, check if you have python installed and also check its version

Open up your terminal and type python. if its installed then it will show information like version, help... check your version (mine is python 2.7.9)

aman@vostro:~$ python Python 2.7.9 (default, Apr 2 2015, 15:33:21) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.

If you don't have python then install it sudo apt-get install python

If you want to install python 3 then enter the following. If you are a newbie, I would recommend python 2 instead of python 3. Python 2 is still very popular and many apps are made on it. On ubuntu python2 is still the default sudo apt-get install python3

Finally, Install Tkinter

sudo apt-get install python-tk

for python 3

sudo apt-get install python3-tk

How to Use it

Now, lets check if Tkinter is working well with this little example

open your terminal and enter into your python shell. python

for python3 python3

if python was installed correctly you will get a >>> prompt. aman@vostro:~$ python

Python 2.7.9 (default, Apr  2 2015, 15:33:21)
[GCC 4.9.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.
>>>

Now import Tkinter module. it wont show any error if it got imported correctly. NOTE: Make sure you type Tkinter (not tkinter) in python2 and tkinter (not Tkinter) in python3.

>>>import Tkinter

Now, just to check you can create an empty window using Tkinter.

>>>Tkinter.Tk()
Sonia Rani
  • 608
  • 9
  • 4
3

The situation on macOS is still a bit complicated, but do-able:

Python.org strongly suggest downloading tkinter from ActiveState, but you should read their license first (hint: don't redistribute or want Support).

When the download is opened OS X 10.11 rejected it because it couldn't find my receipt: "ActiveTcl-8.6.pkg can’t be opened because it is from an unidentified developer".

I followed an OSXDaily fix from 2012 which suggested allowing from anywhere. But OS X has now added an "Open Anyway" option to allow (e.g.) Active-Tcl as a once off, and the "Anywhere" option has gained a timeout.

New warning for "Anywhere"

jalanb
  • 1,097
  • 2
  • 11
  • 37
3

For Fedora >= 25 and python >= 3, we might need to include the dependencies for Tkinter

sudo dnf install python3-tkinter

Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
3

To install the Tkinter on popular Linux distros:

Debian/Ubuntu:

sudo apt install python3-tk -y  

Fedora:

sudo dnf install -y python3-tkinter

Arch:

sudo pacman -Syu tk --noconfirm 

REHL/CentOS6/CentOS7:

sudo yum install -y python3-tkinter

OpenSUSE:

sudo zypper in -y python-tk
amzy-0
  • 435
  • 4
  • 5
2

I think you have to install the tkinter onto your code repository directory.

For liunx (Ubuntu or debian), open the terminal. Install the tkinter package by running this command on the terminal.

sudo apt-get install python-tk 

After installing the python-tk package on the terminal, create a new python file. Call it filename.py.

On the first line of the filename.py, import tkinter into the python file.

import tkinter

To create a tkinter with different fields, you can use this tutorial. https://www.delftstack.com/tutorial/tkinter-tutorial/

The documentation of tkinter can be found here: https://docs.python.org/3.7/library/tkinter.html

Hope that helps. Do ask us if you are still stuck. Remember to identify what you need in the GUI on tkinter before implementing it. Drawing wireframes of how the GUI will look like will help in coding the Tkinter GUI.

Caleb Yang
  • 83
  • 1
  • 12
2

Use ntk for your desktop application, which work on top of tkinter to give you more functional and good looking ui in less codding.

install ntk by pip install ntk

proper Documentation in here: ntk.readthedocs.io

Happy codding.

Nj Nafir
  • 570
  • 3
  • 13
1

Install python version 3.6+ and open you text editor or ide write sample code like this:

from tkinter import *

root = Tk()
root.title("Answer")

root.mainloop()
Ice Bear
  • 2,676
  • 1
  • 8
  • 24
Sarvesh
  • 95
  • 1
  • 13
0

Even after you've successfully imported python3 and python3-tk, it still might not work. I changed the first line of my script from: #! /usr/bin/python to: #! /usr/bin/python3

It finally worked!

Boffin
  • 161
  • 8
0

For Ubuntu users and Linux users in general, it's not a bad idea to read the tips I've found here:

0: Do NOT purge Python from your system otherwise it's very likely you go through hell to re-install the things you will miss, including the ubuntu-desktop.

1: Update your local software repository

sudo apt update

2: Install the stuff everybody needs:

sudo apt install software-properties-common

3: Add the Dead Snake to your repository list:

sudo add-apt-repository ppa:deadsnakes/ppa

4: Update the entire thing again because we installed stuff and added a new repo:

sudo apt update

5: Install the Python version you want:

sudo apt install python3.10

6: Check that the version you want was installed:

python3 --version

Finally, you might want to restart your PC.

Farlon Souto
  • 116
  • 1
  • 6
0

You can simply install it by your package manager(pip). If you haven't installed pip yet you can download the latest version of it.

And to download tkinter

pip install tkinter

And if you are maybe using a few versions of python, you can install it in the specific version you are currently using. if it's 3.10, it is,

pip3.10 install tkinter

Tharushi
  • 43
  • 7
-1

You only need to import it:

import tkinter as tk

then you will be use the phrase tk, which is shorter and easier.

Also, I prefer using messagebox too:

from tkinter import messagebox as msgbx

Here's some ways you will be able to use it.

# make a new window
window = tk.Tk()

# show popup
msgbx.showinfo("title", "This is a text")
Jongwoo Lee
  • 774
  • 7
  • 20