在Linux平台上编译包含tkinter的Python3.3.3

tkinter是一个Python GUI工具。从Python官方网站上可以下载到针对不同操作系统的Python3.3.3,对于Windows平台提供的Python安装包中已经包含了tkinter,对于Linux平台提供的Python安装包中不包含tkinter。用户可以使用如apt或yum之类的工具下载安装tkinter。本文将介绍如何手动编译包含tkinter的Python,前提是Linux中已经安装了GCC编译器。

1. tkinter实际上是由tcl和tk两部分组成,先下载tcl和tk,下载的网址是:http://www.tcl.tk/software/tcltk/download.html

当前二者最新的版本号都是8.6.1。我在下载压缩包后将它们分别解压缩到tcl8.6.1和tk8.6.1目录下,先安装tcl,再安装tk。注意针对Linux平台的源码分别是在 tcl8.6.1/unix 和 tk8.6.1/unix 目录下,安装 tcl 和 tk 的方法是分别进入各自对应的unix目录,然后依次执行以下命令:

./configure

make

make install

2.下载为Linux平台提供的Python3.3.3压缩包,我将其解压缩到Python-3.3.3目录下,然后修改子目录Modules下的Setup.dist文件,找到 # The _tkinter module. 这一行,从该行开始下面的内容与tkinter有关,我修改后的和tkinter有关的那一部分内容如下:

# The _tkinter module.
#
# The command for _tkinter is long and site specific.  Please
# uncomment and/or edit those parts as indicated.  If you don't have a
# specific extension (e.g. Tix or BLT), leave the corresponding line
# commented out.  (Leave the trailing backslashes in!  If you
# experience strange errors, you may want to join all uncommented
# lines and remove the backslashes -- the backslash interpretation is
# done by the shell's "read" command and it may not be implemented on
# every system.

# *** Always uncomment this (leave the leading underscore in!):
 _tkinter _tkinter.c tkappinit.c -DWITH_APPINIT
# *** Uncomment and edit to reflect where your Tcl/Tk libraries are:
 -L/usr/local/lib
# *** Uncomment and edit to reflect where your Tcl/Tk headers are:
 -I/usr/local/include
# *** Uncomment and edit to reflect where your X11 header files are:
 -I/usr/include/X11
# *** Or uncomment this for Solaris:
#    -I/usr/openwin/include \
# *** Uncomment and edit for Tix extension only:
#    -DWITH_TIX -ltix8.1.8.2 \
# *** Uncomment and edit for BLT extension only:
#    -DWITH_BLT -I/usr/local/blt/blt8.0-unoff/include -lBLT8.0 \
# *** Uncomment and edit for PIL (TkImaging) extension only:
#     (See http://www.pythonware.com/products/pil/ for more info)
#    -DWITH_PIL -I../Extensions/Imaging/libImaging  tkImaging.c \
# *** Uncomment and edit for TOGL extension only:
#    -DWITH_TOGL togl.c \
# *** Uncomment and edit to reflect your Tcl/Tk versions:
 -ltk8.6 -ltcl8.6
# *** Uncomment and edit to reflect where your X11 libraries are:
 -L/usr/lib64
# *** Or uncomment this for Solaris:
#    -L/usr/openwin/lib \
# *** Uncomment these for TOGL extension only:
#    -lGL -lGLU -lXext -lXmu \
# *** Uncomment for AIX:
#    -lld \
# *** Always uncomment this; X11 libraries to link with:
 -lX11

 主要的修改之处在于去掉一些行前面的注释符号#,并且指定了和tcl、tk、X11有关的头文件路径和库文件版本、路径。注意不同的Linux平台上这些路径很可能不一样,我用的操作系统是Oracle Linux6.2,读者不可以照抄上面的修改,而是要根据自己的情况来做修改。如果不清楚动态库或头文件路径的话,可以用 find 命令查找一下,比如可以用命令 find / -name libtcl8.6.so 找到 libtcl8.6 所在的路径,在我的电脑上 libtcl8.6.so 和 libtk8.6.so 是在 /usr/local/lib 目录下,X11的库文件在/usr/lib64 目录下。

    修改完成以后存盘。到Python-3.3.3目录下依次执行 ./configure 命令(这里可以使用./configure --prefix= 指定安装路径,例如设为 ./configure --prefix=/usr/local/python3),执行 make 命令,结果报错:build _tkinter failed! 出错原因是找不到 libtk8.6.so文件,执行到这里就停下来了。

   由于已经在Setup.dist文件中设置过库文件路径为 /usr/local/lib,不知道为什么不起作用?原因一直没有找到,但是找到解决办法了:用文本编辑器打开 /etc/ld.so.conf 文件,在我的电脑上显示的内容只有一行:include ld.so.conf.d/*.conf

追加一行,内容是 /usr/local/lib

存盘,然后执行命令 ldconfig

回到Python-3.3.3目录下,先用命令 make clean 清除上次编译得到的结果,然后依次执行 make 和 make install 命令,这次没有报错了。

安装完成以后,为了测试能否使用tkinter,从网上找到两个小程序,分别重命名位test1.py和test2.py,放到Python-3.3.3目录下。注意对网上找到的程序要略做修改,修改之处是把原来程序中的Tkinter都改为tkinter,因为在Python3.3中都要写成tkinter。

第一个小测试程序test1.py:

import tkinter
top = tkinter.Tk()
quit = tkinter.Button(top, text='Hello world!', command = top.quit, bg='blue', fg='white')
quit.pack()
label = tkinter.Label(top, text='Hello World!')
label.pack()
tkinter.mainloop()

第二个小测试程序test2.py:
from tkinter import *
def resize(ev=None):
    label.config(font='Helvetica -%d bold' % scale.get())
top=Tk() 
top.geometry('600x400') 
label=Label(top,text='Hello world!',font='Helvetica -12 bold') 
label.pack(fill=Y,expand=1)
scale=Scale(top,from_=10,to=40,orient=HORIZONTAL,command=resize)
scale.set(12) 
scale.pack(fill=X,expand=1)
quit = Button(top,text='QUIT',command=top.quit,activeforeground='white',activebackground='red')
quit.pack()
mainloop()
分别执行 ./python test1.py 和 ./python test2.py

两个小程序都能够正常运行。

在python前面加上 ./ 的原因是在我的系统中原先安装有 Python 2.7.6,Python 2.7.6 中未包含tkinter,所以如果执行命令 python test1.py 和 python test2.py 都会报告ImportError: No module named tkinter 错误。如果系统中原来装有Python,按照本文介绍的方法安装Python3.3.3存在覆盖原有老版本Python的可能性,但也有可能不会覆盖。可以参考网上的一篇文章来避免这个问题:http://www.cnblogs.com/lanxuezaipiao/archive/2012/10/21/2732864.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值