在Python3中安装Tkinter,需要对Python版本进行升级,重新编译即可!!!
[root@server1 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.2 (Maipo)
Python版本:2.7.5
tkinter是由tcl和tk两个部分组成的
tcl:工具控制语言(Tool Command Language)
tk:Tcl“图形工具箱”的扩展,它提供各种标准的 GUI 接口项,以利于迅速进行高级应用程序开发
官网链接:https://wiki.python.org/moin/TkInter/
下载地址:https://www.tcl.tk/software/tcltk/download.html
[root@server1 ~]# find / -name "*tkinter.so" ###找不到结果
[root@server1 ~]# wget https://prdownloads.sourceforge.net/tcl/tcl8.6.8-src.tar.gz
[root@server1 ~]# wget https://prdownloads.sourceforge.net/tcl/tk8.6.8-src.tar.gz
[root@server1 ~]# tar xf tcl8.6.8-src.tar.gz
[root@server1 ~]# cd tcl8.6.8/unix/
[root@server1 unix]# ./configure
[root@server1 unix]# make && make install
[root@server1 ~]# tar xf tk8.6.8-src.tar.gz
[root@server1 ~]# cd tk8.6.8/unix
[root@server1 unix]# ./configure
[root@server1 unix]# make && make install
(2)报错:
In file included from /root/tk8.6.8/unix/../generic/tkPort.h:21:0,
from /root/tk8.6.8/unix/../generic/tkInt.h:19,
from /root/tk8.6.8/unix/../generic/tkStubLib.c:14:
/root/tk8.6.8/unix/../generic/tk.h:96:25: fatal error: X11/Xlib.h: No such file or directory
# include
解决方案:
[root@server1 unix]# yum install -y libX11-devel
[root@server1 unix]# make && make install
[root@server1 unix]# ll /usr/local/lib|grep tk
-r-xr-xr-x 1 root root 1545763 Oct 16 10:35 libtk8.6.so
-rwxr-xr-x 1 root root 5944 Oct 16 10:35 libtkstub8.6.a
drwxr-xr-x 6 root root 4096 Oct 16 10:35 tk8.6
-rw-r--r-- 1 root root 4070 Oct 16 10:35 tkConfig.sh
[root@server1 ~]# yum install -y tcl-devel tk-devel ###可以发现我们装的是8.5版本8.5.13
[root@server1 ~]# find / -name "libtk*" ###/usr/lib64/libtk8.5.so为8.5版本,因此我们的版本为8.5
/root/tk8.6.8/unix/libtkstub8.6.a
/root/tk8.6.8/unix/libtk8.6.so
/usr/lib64/libtk8.5.so
/usr/local/lib/libtk8.6.so
/usr/local/lib/libtkstub8.6.a
python官网:https://www.python.org/
下载地址:https://www.python.org/downloads/
[root@server1 ~]# wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz
###安装依赖, sqlite和 openssl:否则Django会报错
[root@server1 ~]# yum install -y gcc zlib-devel sqlite-devel openssl-devel libffi-devel
[root@server1 ~]# tar xf Python-3.6.0.tar.xz
[root@server1 Python-3.6.0]# cd Python-3.6.0/
[root@server1 Python-3.6.0]# vim Modules/Setup.dist
###打开下面几项,指定位置即为你系统中真实存在目录,如果只是升级python版本不用进行修改
269 _tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \
271 -L/usr/local/lib \
273 -I/usr/local/include \
298 -lX11
###此项改为我们对应得版本号
288 -ltk8.5 -ltcl8.5 \
[root@server1 Python-3.6.0]# ./configure --with-ssl
[root@server1 Python-3.6.0]# make && make install
[root@server1 ~]# which python
/usr/bin/python
[root@server1 ~]# ll /usr/bin/python
lrwxrwxrwx. 1 root root 7 Oct 9 20:36 /usr/bin/python -> python2
[root@server1 ~]# ll /usr/bin/python*
lrwxrwxrwx 1 root root 22 Oct 16 11:04 /usr/bin/python -> /usr/local/bin/python3
lrwxrwxrwx. 1 root root 9 Oct 9 20:36 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Oct 12 2015 /usr/bin/python2.7
[root@server1 Python-3.6.0]# ll /usr/local/bin/python*
lrwxrwxrwx 1 root root 9 Oct 16 11:00 /usr/local/bin/python3 -> python3.6
-rwxr-xr-x 2 root root 12791811 Oct 16 10:59 /usr/local/bin/python3.6
lrwxrwxrwx 1 root root 17 Oct 16 11:00 /usr/local/bin/python3.6-config -> python3.6m-config
-rwxr-xr-x 2 root root 12791811 Oct 16 10:59 /usr/local/bin/python3.6m
-rwxr-xr-x 1 root root 3097 Oct 16 11:00 /usr/local/bin/python3.6m-config
lrwxrwxrwx 1 root root 16 Oct 16 11:00 /usr/local/bin/python3-config -> python3.6-config
[root@server1 ~]# rm -f /usr/bin/python
[root@server1 ~]# ln -s /usr/local/bin/python3 /usr/bin/python
[root@server1 ~]# python --version
Python 3.6.0
升级后会发现yum无法使用!!!
$ yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
/usr/bin/yum
[root@server1 ~]# vim /usr/bin/yum
[root@server1 ~]# vim /usr/libexec/urlgrabber-ext-down
将第一行"#!/usr/bin/python" 改为 "#!/usr/bin/python2.7"或者"#!/usr/bin/python2"即可,即自己本身的Python版本
sed -i '/python$/s#python#python2.7#' /usr/bin/yum
sed -i '/python$/s#python#python2.7#' /usr/libexec/urlgrabber-ext-down
sed -i 's#python#python2.7#' /usr/share/createrepo/genpkgmetadata.py
sed -i 's#python#python2.7#' /usr/share/createrepo/worker.py
sed -i 's#python#python2.7#' /usr/bin/yum-config-manager
sed -i 's#python#python2.7#' /usr/bin/yum-debug-restore
sed -i 's#python#python2.7#' /usr/bin/yum-groups-manager
sed -i 's#python#python2.7#' /usr/bin/yum-builddep
sed -i 's#python#python2.7#' /usr/bin/yum-debug-dump
sed -i 's#python#python2.7#' /usr/bin/yumdownloader
sed -i 's#python#python2.7#' /usr/libexec/urlgrabber-ext-down
[root@server1 ~]# python
Python 3.6.0 (default, Oct 16 2018, 09:27:46)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
[root@server1 ~]# wget https://files.pythonhosted.org/packages/86/cf/bb010f16cefa8f26ac9329ca033134bcabc7a27f5c3d8de961bacc0f80b3/xlrd-1.1.0.tar.gz
[root@server1 ~]# wget https://files.pythonhosted.org/packages/06/97/56a6f56ce44578a69343449aa5a0d98eefe04085d69da539f3034e2cd5c1/xlwt-1.3.0.tar.gz
### xlrd安装
[root@server1 ~]# tar xf xlrd-1.1.0.tar.gz
[root@server1 ~]# cd xlrd-1.1.0/
[root@server1 xlrd-1.1.0]# python setup.py install
### xlwt安装
[root@server1 ~]# tar xf xlwt-1.3.0.tar.gz
[root@server1 ~]# cd xlwt-1.3.0/
[root@server1 xlwt-1.3.0]# python setup.py install
[root@server1 xlwt-1.3.0]# python
Python 3.6.0 (default, Oct 15 2018, 11:02:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlrd
>>> import xlwt