我创建了一个简单的图像打开程序,该程序通过单击一个按钮来打开从文件对话框中选择的图像,但是无论我选择哪个图像,它都只会出现在当前图像下
我希望将下一个图像替换为旧图像。
请帮助我该怎么办
from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
root=Tk()
root.title('Image')
def open():
global my_img
root.filename = filedialog.askopenfilename(initialdir='/GUI',title='Select A File',filetypes=(('jpg files','*.jpg'),('png files','*.png'),('all files','*.*')))
my_img = ImageTk.PhotoImage(Image.open(root.filename))
my_image_lbl = Label(image=my_img).pack()
my_btn = Button(root,text='Open File Manager',command=open).pack()
root.mainloop()
您应该创建my_image_lbl
外部open()
并在函数内部更新其图像:
from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
root=Tk()
root.title('Image')
def open():
filename = filedialog.askopenfilename(initialdir='/GUI',title='Select A File',filetypes=(('jpg files','*.jpg'),('png files','*.png'),('all files','*.*')))
if filename:
my_image_lbl.image = ImageTk.PhotoImage(file=filename)
my_image_lbl.config(image=my_image_lbl.image)
Button(root,text='Open File Manager',command=open).pack()
my_image_lbl = Label(root)
my_image_lbl.pack()
root.mainloop()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句