在公司机器上,最简单的方法(如果您没有权限)是进行自定义构建。
为了使用 构建python
,tkinter
您需要先安装tcl
和tk
。gcc
我用and构建一切g++
。
从这里下载tcl
和(tk
src.tar.gz
确保版本相同):https ://www.tcl.tk/software/tcltk/download.htmltcl
tk
# Unpack tcl
mkdir tcl_install
tar -zxvf tcl* --directory tcl_install
# configure
# say the absolute path where you want to install tcl is saved in INSTALLDIR_TCL
mkdir $INSTALLDIR_TCL # our install folder
cd tcl_install/*/unix
./configure --prefix=$INSTALLDIR_TCL CC=gcc CXX=g++ --enable-threads --enable-shared
# make install
make
make install
# cleanup
rm -rf tcl_install
和同样的事情tk
,但这次指定你安装的位置tcl
:
# Unpack tk
mkdir tk_install
tar -zxvf tk* --directory tk_install
# configure
# say the absolute path where you want to install tcl is saved in INSTALLDIR_TK
mkdir $INSTALLDIR_TK # our install folder
cd tk_install/*/unix
./configure --prefix=$INSTALLDIR_TK --with-tcl=$INSTALLDIR_TCL/lib CC=gcc CXX=g++ --enable-threads --enable-shared
# make install
make
make install
# cleanup
rm -rf tk_install
现在我们构建python(在此处下载源代码)。我们必须指定安装位置(在下面的代码中编辑版本)tcl
。tk
此外,在调用 之前make
,我们需要指定 和 的标头在tcl
哪里tk
。
# Unpack python
mkdir python_install
tar -zxvf Python* --directory python_install
# configure
# say the absolute path where you want to install tcl is saved in INSTALLDIR_PYTHON
mkdir $INSTALLDIR_PYTHON # our install folder
cd python_install
./configure --prefix=$INSTALLDIR_PYTHON CC=gcc CXX=g++ --with-tcltk-includes="-I$INSTALLDIR_TCL/include -I$INSTLLDIR_TK/include" --with-tcltk-libs="$INSTALLDIR_TCL/lib/libtcl8.6.so $INSTALLDIR_TK/lib/libtk8.6.so"
# make install
export CPPFLAGS="-I$INSTALLDIR_TCL/include -I$INSTALLDIR_TK/include"
make
make install
# cleanup
rm -rf python_install
最后,将tcl
和tk
库文件夹复制到python/lib
:
# copy tcl tk libs to python
cp -r tcl/lib/tcl8.6/ python/lib
cp -r tk/lib/tk8.6/ python/lib
要使用 python 二进制文件tkinter
并避免 message cannot import tkinter
,您必须LD_LIBRARY_PATH
在启动应用程序之前导出:
export LD_LIBRARY_PATH=/path_to_your_python/python/tcl/lib:/path_to_your_python/python/tk/lib:$LD_LIBRARY_PATH
/path_to_your_python/python/bin/python3.7 your_app.py # or wathever your python binary is