Python 如何销毁小部件?

Python 如何销毁小部件?,python,button,tkinter,widget,destroy,Python,Button,Tkinter,Widget,Destroy,我想在我的if语句中让它破坏我的tkinter上的按钮。我尝试了几种方法,查找了一些我不理解/太复杂的方法。我尝试过让函数创建一个新窗口,但它没有显示 def greenwin(): global tkinter global Tk root = Tk() root.title("GAME OVER") root.geometry('387x387') gamelabel=Label(root,text="GAME OVER!GREENS WIN!

我想在我的if语句中让它破坏我的tkinter上的按钮。我尝试了几种方法,查找了一些我不理解/太复杂的方法。我尝试过让函数创建一个新窗口,但它没有显示

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
我想要一个清晰的销毁小部件的方法。我想要一个销毁所有这些按钮的函数

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()

是不是真的是widget.destroy?@piintesky我会上传我的代码now@Nae不,它不起作用这听起来像个XY问题。通常,当我们遇到这个问题时,真正的问题是如何更改显示的帧?,它有一个完全不同的答案。请扩展您的问题,以包括用户应该体验到的总体情况,并包括一个问题。@Gabelvey当然可能不会,但您提出问题的方式也可能不会。我们可以继续猜测你的代码是什么样子的,或者你可以准备一个答案,然后得到一个明确而快速的答案。你忘记了bind和root.mainloop evt位是什么@7stud@Novel“我该把绳子绑在哪里?”小说,谢谢。复制/粘贴错误-剪切最后3行。固定的
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()