导入错误:没有名为_imagingtk的模块

6 投票
4 回答
12329 浏览
提问于 2025-04-18 00:51

我想开始用Python的Tkinter库,所以写了以下代码:

#!/usr/bin/python

from Tkinter import *
from PIL import ImageTk, Image

top = Tk()
dir(top)
top.title("Erstes Frame")

erstesFrame = Frame(top, height=250, width=250)
erstesFrame.pack_propagate(0)
erstesFrame.pack()

img = ImageTk.PhotoImage(Image.open("mario.gif"))

erstesBild = Label(erstesFrame, image = img)

erstesBild.pack()

top.mainloop()

但是当我尝试运行这段代码时,出现了这个错误:

Traceback (most recent call last):
  File "ToDoAPP.py", line 14, in <module>
    img = ImageTk.PhotoImage(Image.open("mario.gif"))
  File "/usr/local/lib/python2.7/dist-packages/PIL/ImageTk.py", line 116, in __init__
    self.paste(image)
  File "/usr/local/lib/python2.7/dist-packages/PIL/ImageTk.py", line 181, in paste
    import _imagingtk
ImportError: No module named _imagingtk

我用python-pip安装了PIL库,我的操作系统是ubuntu 12.04,Python版本是2.7.3。

4 个回答

0

最通用的解决方案是:

python -m pip install image

这个方法应该适用于Python 2和Python 3,也能在Windows系统上使用。使用PIP可以自动安装所有需要的依赖项。

0

补充一下这里的回答,在Ubuntu 16.04上,软件包的名字稍微有点不同(即使是Python 2):

sudo apt-get install python-pil.imagetk

4

正如falsetru所提到的,你需要先安装ImageTk模块:这个模块不会自动随Python一起安装。要安装它,你可以运行下面的命令:

如果你使用的是Python 2.x版本,使用这个命令:

sudo apt-get install python-imaging-tk

如果你使用的是Python 3.x版本,使用这个命令:

sudo apt-get install python3-pil.imagetk
18

你需要安装 ImageTk 模块。

在 Debian 或 Ubuntu 系统上,你可以使用以下命令来安装它:

sudo apt-get install python-imaging-tk

更新

如果你使用的是较新的 Ubuntu 版本(16.04 及以上),那么软件包的名称已经改变。

  • python-pil.imagetk(适用于 Python 2.x)
  • python3-pil.imagetk(适用于 Python 3.x)

撰写回答