python not configured_Tkinter: “Python may not be configured for Tk”

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值