我想在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
答案 0 :(得分:1)
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()
答案 1 :(得分:0)
试试这个:
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()