Python Tkinter-如何在Button中显示JPG图像 [英] Python Tkinter - How to display JPG image in Button

查看:406
本文介绍了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.

这篇关于Python Tkinter-如何在Button中显示JPG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆