图片未在 python 中显示 UI 中的 Tkinter
Image not displaying in python Tkinter in UI
我正在尝试使用 Tkinter 模块在 python 2.7 中制作一个简单的 UI 来浏览和 select 图像并将其显示在 UI 上,但是当我浏览时和 select 图像,然后屏幕上的当前图像(最初加载的图像)变得清晰,但 selected 图像不会显示在屏幕上。
from Tkinter import *
from PIL import ImageTk, Image
import tkFileDialog
root = Tk()
root.title('Simple Image Display app')
w = Canvas(root, width=1100, height=600)
w.pack()
root.resizable(width=FALSE, height=FALSE)
img = ImageTk.PhotoImage(Image.open('test_2.JPG').convert('LA'))
panel = Label(root, image = img)
panel.place(x=700,y=100)
def Open(): ## function to open file dialog and select the file to use in the reader application
dialog = Tk()
dialog.withdraw()
fname = tkFileDialog.askopenfilename(filetypes = (("Image Files", "*.JPG"), ("All files", "*")))
dialog.destroy()
img = ImageTk.PhotoImage(Image.open(fname).convert('LA'))
panel.configure(image = img)
menubar = Menu(root)
# File Menu
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=Open)
menubar.add_cascade(label="File", menu=filemenu)
# display the menu
root.config(menu=menubar)
root.mainloop()
在解决问题方面有任何帮助或建议吗?
提前致谢。
之前好像有人在 SO 上遇到过这个问题:How to update the image of a Tkinter Label widget?
它归结为 Tkinter
在图像上执行 garbage collection
所以它超出了范围。您想要在 panel.configure(...)
行下方添加 panel.image = img
。
根据@nicholas-smith 的建议更改后,我的代码运行良好。
这是 python 2.7
的更新和工作代码
def Open(): ## function to open file dialog and select the file to use in the reader application
dialog = Tk()
dialog.withdraw()
fname = tkFileDialog.askopenfilename(filetypes = (("Image Files", "*.JPG"), ("All files", "*")))
dialog.destroy()
img2 = ImageTk.PhotoImage(Image.open(fname).convert('LA'))
panel.configure(image = img2)
panel.image = img2
只需要修改“打开”功能
我正在尝试使用 Tkinter 模块在 python 2.7 中制作一个简单的 UI 来浏览和 select 图像并将其显示在 UI 上,但是当我浏览时和 select 图像,然后屏幕上的当前图像(最初加载的图像)变得清晰,但 selected 图像不会显示在屏幕上。
from Tkinter import *
from PIL import ImageTk, Image
import tkFileDialog
root = Tk()
root.title('Simple Image Display app')
w = Canvas(root, width=1100, height=600)
w.pack()
root.resizable(width=FALSE, height=FALSE)
img = ImageTk.PhotoImage(Image.open('test_2.JPG').convert('LA'))
panel = Label(root, image = img)
panel.place(x=700,y=100)
def Open(): ## function to open file dialog and select the file to use in the reader application
dialog = Tk()
dialog.withdraw()
fname = tkFileDialog.askopenfilename(filetypes = (("Image Files", "*.JPG"), ("All files", "*")))
dialog.destroy()
img = ImageTk.PhotoImage(Image.open(fname).convert('LA'))
panel.configure(image = img)
menubar = Menu(root)
# File Menu
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=Open)
menubar.add_cascade(label="File", menu=filemenu)
# display the menu
root.config(menu=menubar)
root.mainloop()
在解决问题方面有任何帮助或建议吗?
提前致谢。
之前好像有人在 SO 上遇到过这个问题:How to update the image of a Tkinter Label widget?
它归结为 Tkinter
在图像上执行 garbage collection
所以它超出了范围。您想要在 panel.configure(...)
行下方添加 panel.image = img
。
根据@nicholas-smith 的建议更改后,我的代码运行良好。 这是 python 2.7
的更新和工作代码def Open(): ## function to open file dialog and select the file to use in the reader application
dialog = Tk()
dialog.withdraw()
fname = tkFileDialog.askopenfilename(filetypes = (("Image Files", "*.JPG"), ("All files", "*")))
dialog.destroy()
img2 = ImageTk.PhotoImage(Image.open(fname).convert('LA'))
panel.configure(image = img2)
panel.image = img2
只需要修改“打开”功能