使用 tkinter 显示所选图像 [英] displaying the selected image using tkinter

查看:54
本文介绍了使用 tkinter 显示所选图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以显示我选择的路径中的图像?比如,我有一个路径,例如:c:\user\desktop\33.jpg,我只想获取那个 jpg 文件,我必须使用标签或其他东西来显示该图像.如果可以的话,我想知道怎么做?

I want to know whether I can display an image from the path I have selected? like, I have a path for example: c:\user\desktop\33.jpg, and I want to take only that jpg file and I have to display that image using label or something. If it is possible, I want to know how?

提前致谢!

推荐答案

以下是您所问的示例代码:

Here is a sample code for what you are asking:

from Tkinter import Label,Tk
from PIL import Image, ImageTk
import tkFileDialog
root = Tk()

path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
im = Image.open(path)
tkimage = ImageTk.PhotoImage(im)
myvar=Label(root,image = tkimage)
myvar.image = tkimage
myvar.pack()

root.mainloop()

您会想要添加一个按钮来调用askopenfilename,因为现在它会在程序开始时调用它.此外,您可能想向 filetypes

You will be wanting to add a button for calling the askopenfilename because right now its calling it the moment the program begins. Also you might wanna add more file extensions to filetypes

这篇关于使用 tkinter 显示所选图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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