【问题标题】:How to use a PIL image in tkinter如何在 tkinter 中使用 PIL 图像
【发布时间】:2020-04-19 10:45:37
【问题描述】:

所以我正在尝试使用 PIL 打开图像,调整其大小,然后将其放在 tkinter 画布上。我试过导入 TkImage:

import TkImage
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'TkImage'

from PIL import TkImage
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'TkImage'

from PIL.Image import TkImage
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'TkImage'

from PIL.Image import Image
i = Image.TkImage()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Image' has no attribute 'TkImage'

所有这些都会引发异常。有人可以帮忙吗?

【问题讨论】:

  • 请在您的问题中添加错误消息/例外情况。
  • 没有任何名称为TkImage。您是否阅读过一些显示名为TkImage 的文档?你想使用ImageTk吗?
  • “行不通”是什么意思?你绝对可以从 PIL 导入ImageTk
  • 请显示错误。不过,仅仅在import 语句中抛出随机字符并不是解决问题的正确方法。有大量文档展示了如何导入 PIL 及其组件。
  • 您确定 PIL 已正确安装在您的系统上吗?在 shell 窗口(Windows 上的命令窗口)中尝试pip install Pillow,并查看(重新)安装过程是否没有错误。

标签: python tkinter python-imaging-library python-3.5


【解决方案1】:

PIL 中没有任何名称为 TkImage。但是,有一个名为 ImageTk 的东西。

from PIL import ImageTk

【讨论】:

    猜你喜欢
    • 2022-06-27
    • 2010-11-17
    • 2013-08-24
    • 2012-04-10
    • 2011-06-18
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多