【问题标题】:Python Tkinter - How to display JPG image in ButtonPython Tkinter - 如何在按钮中显示 JPG 图像
【发布时间】:2013-09-14 15:40:55
【问题描述】:

使用 python 2.7 和 Tkinter,我如何制作一个图像,当单击它时运行一个函数? 我正在考虑使用类似的东西

Button(...,foto = "mypic.jpg",command=myfunc)

但我不会。

非常感谢您的帮助!

【问题讨论】:

  • 我遇到了同样的问题。 TKinter 官方文档(此处:effbot.org/tkinterbook/button.htm)说应该可以,但我还没有发现它在 Python 2.7 中有效(寻找复合选项):(

标签: python tkinter jpeg


【解决方案1】:

环顾四周后,这就是我得到的。不过,我还没有测试过。

from PIL import Image, ImageTk

image = Image.open("mypic.jpg")
photo = ImageTk.PhotoImage(image)

Button(...,image=photo,command=myfunc)

这里的foto 应该是imagePIL 是您必须获得的依赖项。希望这会有所帮助。

【讨论】:

  • AttributeError: class Image 没有属性'open'
【解决方案2】:

从 PIL 导入图像,ImageTk

image = Image.open("mypic.jpg") 照片 = ImageTk.PhotoImage(图像)

按钮(...,image=photo,command=myfunc)

【讨论】:

  • 欢迎来到 SO。请不要复制和粘贴与之前发布的完全相同的答案。
猜你喜欢
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多