136

My Idle is throwing errors that and says tkinter can't be imported.

Is there a simple way to install tkinter via pip or easy_install?

There seem to be a lot of package names flying around for this...

This and other assorted variations with tkinter-pypy aren't working.

pip install python-tk

I'm on Windows with Python 2.7 and I don't have apt-get or other system package managers.

14 Answers 14

121

The Tkinter library is built-in with every Python installation. And since you are on Windows, I believe you installed Python through the binaries on their website?

If so, Then most probably you are typing the command wrong. It should be:

import Tkinter as tk

Note the capital T at the beginning of Tkinter.

For Python 3,

import tkinter as tk
4
  • 2
    import Tkinter as tk gets: Traceback (most recent call last): File "<interactive input>", line 1, in <module> File "C:\Python27\lib\lib-tk\Tkinter.py", line 38, in <module> import FixTk File "C:\Python27\lib\lib-tk\FixTk.py", line 65, in <module> import _tkinter ImportError: DLL load failed: %1 is not a valid Win32 application. Nov 18, 2013 at 10:04
  • 7
    For the import statement: Python 2.* uses Tkinter with capital 'T', Python 3.*: uses tkinter with small 't'.
    – robsn
    Jan 10, 2015 at 18:11
  • This works for me on Windows 10 and python 3.7.0 on 64 bit
    – Ole_S
    Jan 7, 2019 at 6:15
  • 1
    Easy and Better answer @IcyFlame
    – Jason Li
    Jun 4, 2019 at 13:56
54

If you are using virtualenv, it is fine to install tkinter using sudo apt-get install python-tk(python2), sudo apt-get install python3-tk(python3), and and it will work fine in the virtual environment

3
  • 2
    But this doesn't work if we don't have sudo access and are inside our own virtualenv. Jun 16, 2017 at 4:28
  • 1
    "sudo apt-get install python-tk" worked for me WHILE inside my virtualenv and fixed my issue. Thanks!
    – Thom Ives
    Sep 26, 2017 at 18:34
  • 13
    That doesn't answer the question since the OP is working on windows. The answer belongs to a linux question.
    – Frederik.L
    Feb 16, 2018 at 20:45
49

Well I can see two solutions here:

1) Follow the Docs-Tkinter install for Python (for Windows):

Tkinter (and, since Python 3.1, ttk) are included with all standard Python distributions. It is important that you use a version of Python supporting Tk 8.5 or greater, and ttk. We recommend installing the "ActivePython" distribution from ActiveState, which includes everything you'll need.

In your web browser, go to Activestate.com, and follow along the links to download the Community Edition of ActivePython for Windows. Make sure you're downloading a 3.1 or newer version, not a 2.x version.

Run the installer, and follow along. You'll end up with a fresh install of ActivePython, located in, e.g. C:\python32. From a Windows command prompt, or the Start Menu's "Run..." command, you should then be able to run a Python shell via:

% C:\python32\python

This should give you the Python command prompt. From the prompt, enter these two commands:

>>> import tkinter
>>> tkinter._test()

This should pop up a small window; the first line at the top of the window should say "This is Tcl/Tk version 8.5"; make sure it is not 8.4!

2) Uninstall 64-bit Python and install 32 bit Python.

3
  • for import _tkinter I get: Traceback (most recent call last): File "<interactive input>", line 1, in <module> ImportError: DLL load failed: %1 is not a valid Win32 application. Nov 18, 2013 at 9:52
  • Python 2.7.5 on 64 bit Nov 18, 2013 at 10:03
  • 1
    For python 3 the current version is 8.6 Jun 11, 2018 at 2:20
26

When installing make sure that under Tcl/Tk you select Will be installed on hard drive. If it is installing with a cross at the left then Tkinter will not be installed.

enter image description here

The same goes for Python 3:

enter image description here

1
  • 1
    Thanks for this reminder! I'd completely forgotten those two were coupled.
    – Energya
    Sep 9, 2018 at 11:01
18

When you install python for Windows, use the standard option or install everything it asks. I got the error because I deselected tcl.

2
  • 3
    That's it! I deselected "tk/tcl and IDLE" option during installation because I didn't want IDLE. I reinstalled python and tkinter is detected. Oct 23, 2017 at 17:11
  • 13
    If this is the case, tkinter can be added by modifying the installation and ticking tk/tcl
    – Prof
    Jan 8, 2018 at 20:15
7

I came here looking for an answer to this same question and none of the answers above actually answer the question at all!

So after some investigation I found out: there is a package (for python 3.x at least):

pip3 install pytk

The problem is, it is only the python part of the equation and doesn't install the tkinter libraries in your OS, so the answer is that you can't install it completely via pip https://tkdocs.com/tutorial/install.html

Personally I find this very annoying as i'm packaging a python application to be installed via pip that uses tkinter and I was looking for a way to have pip ensure that tkinter is installed and the answer is I can't I have to instruct users to install it if it's not installed already, a very poor experience for end users who should not need to know or care what tkinter is to use my application.

1
  • The pytk package does not install anything relevant. It is only a wrapper designed to give a more Pythonic interface to the tkinter standard library (which is designed to mimic the Tcl/Tk interface). Mar 3 at 19:57
4

Had the same problem in Linux. This solved it. (I'm on Debian 9 derived Bunsen Helium)

$ sudo apt-get install python3-tk

3
  • This worked for me on my Ubuntu 18.04 server but why? Do linux python distributions not come with tkinter? Sep 15, 2019 at 0:37
  • For a long time, they only came with Python 2. Sep 16, 2019 at 8:04
  • 2
    Doesnt answer the question — OP is running Windows. Jun 19, 2020 at 23:20
3

In python, Tkinter was a default package, you can repair the installation and select Tcl/Tk. repair When you run this, DDL should be installed like so: enter image description here

0
2

I'm posting as the top answer requotes the documentation which I didn't find useful.

tkinter comes packaged with python install on windows IFF you select it during the install window.

The solution is to repair the installation (via uninstall GUI is fine), and select to install tk this time. You may need to point at or redownload the binary in this process. Downloading directly from activestate did not work for me.

This is a common problem people have on windows as it's easy to not want to install TCL/TK if you don't know what it is, but Matplotlib etc require it.

1
  • 1
    FWIW, matplotlib can be used with PyQt5 (and several other graphics backends) instead of tkinter, and PyQt5 is neatly and completely installable via pip install pyqt5. Mar 30, 2021 at 11:28
1

I was trying to upgrade my tkinter from the python built-in and found this method useful:

conda install -c anaconda tk
0

I had the similar problem with Win-8 and python-3.4 32 bit , I got it resolved by downloading same version from python.org .

Next step will be to hit the repair button and Install the Tk/tkinter Package or Just hit the repair. Now should get Python34/Lib/tkinter Module present. The import tkinter should work ..

-3

Easiest way to do this:

cd C:\Users\%User%\AppData\Local\Programs\Python\Python37\Scripts> 
pip install pythonds 

Screenshot of installing

1
  • code on your screenshot is not the code you've added
    – olli_kahn
    Jul 26, 2021 at 14:03
-4

Inside cmd, run command pip install tk and Tkinter should install.

2
-7

if your using python 3.4.1 just write this line from tkinter import * this will put everything in the module into the default namespace of your program. in fact instead of referring to say a button like tkinter.Button you just type Button

2
  • 4
    from Tkinter import *
    – TanuAD
    Jun 13, 2016 at 14:31
  • 9
    The question is asking about how to install tkinter, not how to import it into a program. Mar 15, 2017 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.