如何销毁小部件?

How to destroy widgets?

我希望在我的 if 语句中销毁我的 tkinter 上的按钮。我尝试了几种方法并查找了一些方法,有些我 understand/too 并不复杂。我试过让函数创建一个新的 window 但它没有显示。

def greenwin():
    global tkinter
    global Tk
    root = Tk()
    root.title("GAME OVER")
    root.geometry('387x387')
    gamelabel=Label(root,text="GAME OVER!GREENS
WIN!",width=33,height=15).place(x=150,y=150)
    root.mainloop
    return

我想要一个明确的销毁方法widgets.I想要一个销毁我的井字游戏所有这些按钮的功能。

but1=Button(root,text="",bg="white",width=11,height=5,command=colour1).place(x=0,y=0)
but2=Button(root,text="",bg="white",width=11,height=5,command=colour2).place(x=0,y=150)
but3=Button(root,text="",bg="white",width=11,height=5,command=colour3).place(x=0,y=300)
but4=Button(root,text="",bg="white",width=11,height=5,command=colour4).place(x=150,y=0)
but5=Button(root,text="",bg="white",width=11,height=5,command=colour5).place(x=150,y=150)
but6=Button(root,text="",bg="white",width=11,height=5,command=colour6).place(x=150,y=300)
but7=Button(root,text="",bg="white",width=11,height=5,command=colour7).place(x=300,y=0)
but8=Button(root,text="",bg="white",width=11,height=5,command=colour8).place(x=300,y=150)
but9=Button(root,text="",bg="white",width=11,height=5,command=colour9).place(x=300,y=300)
root.mainloop

试试这个:

import tkinter as tk

root = tk.Tk()
root.geometry("500x300+10+13")
root.title("Test")

b = tk.Button(root, text="click me")

def onclick(evt):
    w = evt.widget
    w.destroy()

b.bind("<Button-1>", onclick)

b.pack()
root.mainloop()
import tkinter as tk


root = tk.Tk()
any_widget = tk.Button(root, text="Press to destroy!")
any_widget['command'] = any_widget.destroy  # pay special attention to the lack of ()
# call any_widget.destroy(), button widget's command option specifically needs a
# reference to the method instead of an actual call
any_widget.pack()
root.mainloop()