今天在学python的时候使用Tkinter包的时候出现以下问题:

1
2
3
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

然后就给python重新安装了一遍 结果发现:

1
2
3
4
5
6
7
8
9
10
11
running install
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers

Python build finished, but the necessary bits to build these modules were not found:
_bsddb _curses _curses_panel
_sqlite3 _tkinter bsddb185
dbm dl gdbm
imageop sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

是少了 Tcl/Tk,于是就直接 apt-get install tcl apt-get install tk 安装了 tcltk 库 ,然后又执行 python setup.py install 安装 python

结果还是不行,还是提示

1
Can't locate Tcl/Tk libs and/or headers

郁闷呀;

然后就又百度了下 找到一篇博客:http://www.linuxdiyf.com/viewarticle.php?id=55587 讲的比较详细:

原来 setup.py 安装的时候要寻找 tcl.htk.h 两个头文件,但是我用 find / -name tcl.h 在整个 linux 范围内都没有找到这个头文件。

再仔细看python.org/topics/tkinter/给出的指导:

You may have to install Tcl and Tk(when using RPM, install the –devel RPM 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.

所以我要安装 tcl-devel(我的系统是 debian 安装的是 tcl-dev)和 tk-devel (我的系统是 debian 安装的是 tk-dev)才能有头文件,安装 tcl/tk,只是把静态或者动态库考到 lib 目录下,只有 tcl-devel(tc-dev)tk-devel(tk-dev) 才会把头文件放到 /usr/include 里边,而 _tkinter 要编译必须找到这些头文件。

然后我就执行了

1
2
3
apt-get install tcl-dev    

apt-get install tk-dev

安装完这两个包后问题就解决了。。。