如何通过点击Play按钮同时销毁Play与Help两个按钮?
解决点击Play按钮同时销毁自身与Help按钮的问题
原代码里的destroy函数只能处理单个按钮的销毁,而且后续通过start_btn.config覆盖了初始绑定的逻辑,导致点击Play按钮只能销毁自身。要实现同时销毁两个按钮,只需新增一个能同时操作两个按钮的函数即可:
from tkinter import * import tkinter.messagebox from random import * window = Tk() window.title("Simon") window.geometry("300x200") label = Label(window, text="Simon Game!", font=("Ariel", 80), bg="CadetBlue3") label.pack() window.configure(bg="CadetBlue3") def click_help_button(): tkinter.messagebox.showinfo("Instructions", "The device creates a series of tones and lights and requires a user to repeat the sequence. If the user succeeds, the series becomes progressively longer and more complex. Once the user fails, the game is over") help_btn = Button(window, width=12, height=2, text="Help", bg="grey",font=("Ariel", 18), command=click_help_button) help_btn.pack(side='bottom') help_btn.place(x=800, y=150) # 新增函数:同时销毁Play按钮和Help按钮 def destroy_both_buttons(): help_btn.destroy() start_btn.destroy() start_btn = Button(window, width=12, height=2, text="Play", bg="grey", font=("Ariel", 18), command=destroy_both_buttons) start_btn.pack(pady=10) start_btn.place(x=525, y=150) red_btn = Button(window, width=35, height=15, bg='red') red_btn.place(x=495, y=270) green_btn = Button(window, width=35, height=15, bg='green') green_btn.place(x=750, y=270) blue_btn = Button(window, width=35, height=15, bg='blue') blue_btn.place(x=495, y=495) yellow_btn = Button(window, width=35, height=15, bg='yellow') yellow_btn.place(x=750, y=495) window.mainloop()
点击Play按钮时,会触发destroy_both_buttons函数,同时完成两个按钮的销毁操作。
内容的提问来源于stack exchange,提问作者lika moyal
相关产品推荐
相关产品推荐

