我想出了一个简单的程序,通过单击按钮将图像加载到窗口框架,并删除现有的图像。
问题:当我使用remove_image命令时,它只删除现有图像,留下将其保留在后面的标签,而不是删除带有图像的标签。任何人都可以告诉我这个代码我做错了什么。拜托,谢谢。
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
from tkinter import Label
import os
import sys
root = Tk()
root.title("Mole Tester")
root.geometry("800x500")
root.resizable(width=True, height=True)
optionframe = Frame (root, width=800, height=450, bg="light gray",
relief=SUNKEN)
optionframe.pack(side=BOTTOM)
load = Label(optionframe, padx=140, pady=125, bg="white", font=('Times new
roman', 10, 'bold'), text="Import Image", relief=RAISED).place (x=10, y=10)
#Image Remove
def clear_label_image():
brand_preview.image = None
imagelabel.destroy()
#Image Load
def openfile():
filename = filedialog.askopenfilename(title='open')
return filename
def load_img():
x = openfile()
img = Image.open(x)
img = img.resize((360, 270), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
imagelabel = Label(optionframe, width=360, height=270, image=img,
relief=RAISED).place(x=10, y=10)
imagelabel.pack()
loadbutton = Button(optionframe, padx=18, pady=1, bd=8, fg="black",
font=('arial', 10, 'bold'), text="Load Image", bg="gray",
command=load_img).place(x=18, y=320)
removebutton = Button(optionframe, padx=30, pady=1, bd=8, fg="black",
font=('arial', 10, 'bold'), text="Remove", bg="gray",
command=clear_label_image).place(x=18, y=365)