Python Tkinter-如何在Button中显示JPG图像

问题描述:

使用python 2.7和Tkinter,我如何制作图像,单击该图像可以运行功能? 我正在考虑使用类似的东西

using python 2.7 and Tkinter, how can i make an image, that when clicked on runs a function? i was thinking about using something like

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

但我没有管理.

非常感谢您的帮助!

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

After looking around a bit, here's what I got. I haven't tested this, though.

from PIL import Image, ImageTk

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

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

此处的foto应该为image. PIL是您必须获取的依赖项.希望这会有所帮助.

The foto should be image here. PIL is a dependency you have to obtain. Hope this helps.