You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过点击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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 10:56:06