Skip to main content
 首页 » 编程设计

python之如何为 PIL Python 库添加 Tkinter 支持

2025年05月04日9sharpest

好吧,按理说 PIL 应该能够自动与 Tkinter 一起工作,但我的不是。我在 Imaging 目录 Tk 目录中找到了这个文本文件。

Using PIL With Tkinter  
 
Starting with 1.0 final (release candidate 2 and later, to be 
precise), PIL can attach itself to Tkinter in flight.  As a result, 
you no longer need to rebuild the Tkinter extension to be able to 
use PIL. 
 
However, if you cannot get the this to work on your platform, you 
can do it in the old way: 
 
* Adding Tkinter support 
 
1. Compile Python's _tkinter.c with the WITH_APPINIT and WITH_PIL 
   flags set, and link it with tkImaging.c and tkappinit.c.  To 
   do this, copy the former to the Modules directory, and edit 
   the _tkinter line in Setup (or Setup.in) according to the 
   instructions in that file. 
 
   NOTE: if you have an old Python version, the tkappinit.c 
   file is not included by default.  If this is the case, you 
   will have to add the following lines to tkappinit.c, after 
   the MOREBUTTONS stuff: 
 
  { 
      extern void TkImaging_Init(Tcl_Interp* interp); 
      TkImaging_Init(interp); 
  } 
 
   This registers a Tcl command called "PyImagingPhoto", which is 
   use to communicate between PIL and Tk's PhotoImage handler. 
 
   You must also change the _tkinter line in Setup (or Setup.in) 
   to something like: 
 
   _tkinter _tkinter.c tkImaging.c tkappinit.c -DWITH_APPINIT 
  -I/usr/local/include -L/usr/local/lib -ltk8.0 -ltcl8.0 -lX11 

不幸的是,我不知道该怎么做,而且我在网上找不到任何指南。有人可以指导我如何使用标志集等编译 Python 的 _tkinter.c 吗?

请您参考如下方法:

您使用的是什么版本的 Python 和 PIL(以及在什么平台上等)?所有合理的最新版本都应该已经支持所有这些必需的选项(使用 setup.py 以及 Modules/Setup.dict)——例如,包括 tkappinit.c 等——所以很难“在真空中”知道建议什么”。此外,如何编译东西(假设你确实需要)可能非常依赖于平台,所以如果你确实需要任何重新编译(不太可能),了解你的平台可能很重要。