Install tkinter for Python

2019-01-02 22:32发布

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?

16条回答
相关推荐>>
2楼-- · 2019-01-02 22:38

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
查看更多
相关推荐>>
3楼-- · 2019-01-02 22:39

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.)

查看更多
三岁会撩人
4楼-- · 2019-01-02 22:44

tk-devel also needs to be installed in my case

yum install -y tkinter tk-devel

install these and rebuild python

查看更多
等我变得足够好
5楼-- · 2019-01-02 22:46

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.

查看更多
时光不老,我们不散
6楼-- · 2019-01-02 22:46

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"

查看更多
在下西门庆
7楼-- · 2019-01-02 22:48

For Python 2.7:

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).

as it says here. In my case, on Windows, what helped was reinstalling the Python distribution. Probably 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.

查看更多
登录 后发表回答