Python 空的Tkinter窗口将代替图像显示

Python 空的Tkinter窗口将代替图像显示,python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我试图通过浏览按钮将图像动态加载到Tkinter窗口,但得到的是空窗口。这是浏览按钮回调函数的代码 supformats = [ ('Windows Bitmap','*.bmp'), ('Portable Network Graphics','*.png'), ('JPEG ','*.jpg'), ('CompuServer GIF','*.gif'), ] filename = askopenfilename(filetypes=supformats) FILENAME = filename

我试图通过浏览按钮将图像动态加载到Tkinter窗口,但得到的是空窗口。这是浏览按钮回调函数的代码

supformats = [
('Windows Bitmap','*.bmp'),
('Portable Network Graphics','*.png'),
('JPEG ','*.jpg'),
('CompuServer GIF','*.gif'),
]
filename = askopenfilename(filetypes=supformats)
FILENAME = filename
im=Image.open(FILENAME)
w=im.size[0]
h=im.size[1]
root = Tkinter.Tk()
#canvas = Tkinter.Canvas(root, width=w, height=h)
#canvas.grid(row=0,column=0)
#tk_img = ImageTk.PhotoImage(file = FILENAME)
#canvas.create_image(image=tk_img)
im.show()
root.mainloop()
提前感谢所有从effbot的解释中提供帮助的人注意,该文件只被访问一次,而不是两次。这假设您使用的是Tkinter中的Python映像库,因为您没有说明正在使用什么映像工具包

from PIL import Image, ImageTk

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

You can use a PhotoImage instance everywhere Tkinter accepts an image object.
An example:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
你可能想提到