在我的程序中,我有一个对象startscreencanvas
。它由一个画布,标签小部件和按钮组成。我想删除包括所有小部件的对象。我怎样才能做到这一点?请帮忙!
代码:
class createcanvas:
def __init__(self, master, title, button1text, button1command, button2text, button2command, screen):
master.canvas = Canvas(root, width=610, height=610, bg='#82f2ff', borderwidth=0, highlightthickness=0)
master.canvas.pack()
#
self.titlelabel = Label(master.canvas, image=photo1)
self.titlelabel.place(x=0, y=100, width=600, height=100)
#
self.button1 = Button(master.canvas, text=button1text, command=button1command, relief = 'groove', font=("Ariel", 30))
self.button1.place(x=100, y=450)
#
self.button2 = Button(master.canvas, text=button2text, command=button2command, relief = 'groove', font=("Ariel", 30))
self.button2.place(x=350, y=450)
#
if screen == 'loginscreen':
usernamelabel = Label(master.canvas, image=photo2, relief = 'flat')
usernamelabel.place(x=95, y=250, width=175, height=50)
#
global usernameentry
usernameentry = Entry(master.canvas, relief = 'groove', font=("Ariel", 30))
usernameentry.place(x=275, y=250, width=200, height=50)
#
passwordlabel = Label(master.canvas, image=photo3, relief = 'flat')
passwordlabel.place(x=95, y=300, width=175, height=50)
#
global passwordentry
passwordentry = Entry(master.canvas, relief = 'groove', show="*", font=("Ariel", 30))
passwordentry.place(x=275, y=300, width=200, height=50)
def startscreen():
global startscreencanvas
startscreencanvas = createcanvas(root, 'Maths revision program', "Login", login,
"Register", register, "loginscreen")
startscreen()
root.mainloop()
答案 0 :(得分:1)
如果您将createcanvas
设为tkinter小部件的子类(例如Canvas
),则销毁该实例将销毁所有子部件。
否则,您需要更改代码,以便可以调用内部画布的destroy
方法。由于所有其他小部件都是画布的子级,因此删除画布时,子级小部件将被自动销毁。