Tkinter: “Python may not be configured for Tk”

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

Today I wanted to start working with Tkinter, but I have some problems.

Python 3.2 (r32:88445, Mar 28 2011, 04:14:07)  [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from tkinter import * Traceback (most recent call last): File "", line 1, in  File "/usr/local/lib/python3.2/tkinter/__init__.py", line 39, in  import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named _tkinter 

So how can I configure my Python 3.2 to work with Tkinter?

回答1:

According to http://wiki.python.org/moin/TkInter :

If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.



回答2:

Install tk-devel (or a similarly-named package) before building Python.



回答3:

Under Arch/Manjaro just install the package tk:

sudo pacman -S tk 


回答4:

Had the same issue on Fedora with Python 2.7. Turns out some extra packages are required:

sudo dnf install tk-devel tkinter 

After installing the packages, this hello-world example seems to be working fine on Python 2.7:

$ cat hello.py from Tkinter import * root = Tk() w = Label(root, text="Hello, world!") w.pack() root.mainloop() $ python --version Python 2.7.8 $ python hello.py 

And through X11 forwarding, it looks like this:

Note that in Python 3, the module name is lowercase, and other packages are probably required...

from tkinter import * 


回答5:

Oh I just have followed the solution Ignacio Vazquez-Abrams has suggest which is install tk-dev before building the python. (Building the Python-3.6.1 from source on Ubuntu 16.04.)

There was pre-compiled objects and binaries I have had build yesterday though, I didn't clean up the objects and just build again on the same build path. And it works beautifully.

sudo apt install tk-dev (On the python build path) (No need to conduct 'make clean') ./configure make sudo make install 

That's it!



回答6:

I think the most complete answer to this is the accepted answer found here:

How to get tkinter working with Ubuntu's default Python 2.7 install?

I figured it out after way too much time spent on this problem, so hopefully I can save someone else the hassle.

I found this old bug report deemed invalid that mentioned the exact problem I was having, I had Tkinter.py, but it couldn't find the module _tkinter: http://bugs.python.org/issue8555

I installed the tk-dev package with apt-get, and rebuilt Python using ./configure, make, and make install in the Python2.7.3 directory. And now my Python2.7 can import Tkinter, yay!

I'm a little miffed that the tk-dev package isn't mentioned at all in the Python installation documentation.... below is another helpful resource on missing modules in Python if, like me, someone should discover they are missing more than _tkinter.



回答7:

sudo apt-get install python3-tk 


回答8:

To get this to work with pyenv on Ubuntu 16.04, I had to:

$ sudo apt-get install python-tk python3-tk tk-dev 

Then install the version of Python I wanted:

$ pyenv install 3.6.2 

Then I could import tkinter just fine:

import tkinter 


回答9:

I encounter this issue on python 2.7.9. to fix it, I installed tk and tcl, and then rebuild python code and reinstall, and during configure, I set the path for tk and tcl explicitly, by

./configure --with-tcltk-includes="-I/usr/include" --with-tcltk-libs="-L/usr/lib64 -ltcl8.5 -L/usr/lib64 -ltk8.5"

also, a whole article for python install process: Building Python from Source



回答10:

This symptom can also occur when a later version of python (2.7.13, for example) has been installed in /usr/local/bin "alongside of" the release python version, and then a subsequent operating system upgrade (say, Ubuntu 12.04 --> Ubuntu 14.04) fails to remove the updated python there.

To fix that imcompatibility, one must

a) remove the updated version of python in /usr/local/bin;

b) uninstall python-idle2.7; and

c) reinstall python-idle2.7.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!