如何在按钮按下时清除Tkinter画布

用户

我正在尝试创建一个按钮,它将清除画布,然后将图像放入从Tkinter文件对话框中选择的画布中。我使用canvas.delete("all")方法shared here在选择新图像之前尝试清除画布,但是新图像出现在现有图像的上方,而不是替换它。在

from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
from PIL import Image, ImageTk

root = Tk()

canvas1 = Canvas(root, width=500, height=500)
canvas1.pack()

class widgets:

    def __init__(self, parent):
        self.myParent = parent
        self.myContainer1 = Frame(parent)
        self.myContainer1.pack()

        self.button1 = Button(self.myContainer1, command=self.button1click)
        self.button1.config(text="Select Image")
        self.button1.pack(side=TOP)

    def button1click(self):
        canvas1.delete("all")
        path = tkFileDialog.askopenfilename(filetypes=[("Image File", '.png')])
        im = Image.open(path)
        tkimage = ImageTk.PhotoImage(im)
        imglabel = Label(canvas1, image=tkimage)
        imglabel.image = tkimage
        imglabel.pack()

widgets = widgets(root)
root.mainloop()


已被浏览了1524次
更新日期: 2020-10-28 19:21:23
0 个回答

目前没有回答

最新Python问答

推荐Python问答