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

如何让CustomTkinter按钮移除名字后10秒内无法触发添加?

解决方案

要实现移除名字后10秒内无法重新添加的功能,核心是记录每个用户的冷却状态,并利用CustomTkinter的after()方法实现非阻塞的延迟恢复逻辑,避免整个程序暂停。具体实现如下:

关键修改点

  • 添加cooldown_users字典,存储处于冷却中的用户及其冷却结束时间戳
  • 移除用户名字时启动10秒冷却,同时禁用对应按钮(提升交互直观性)
  • 添加冷却检查逻辑,用户尝试重新添加时先判断是否处于冷却期
  • 修复原代码中的拼写错误(如typAvMeddelande、syudent)

修改后的完整代码

import customtkinter
import time

# 存储活跃用户列表
activeList = []
# 存储冷却中的用户:键为用户名,值为冷却结束时间戳
cooldown_users = {}
# 存储按钮对象,方便后续禁用/启用操作
button_map = {}

def buttons(typeOfMessage, userName):
    userName = userName.strip()  # 去除字符串分割后的多余空格
    current_time = time.time()
    
    if typeOfMessage == "student":
        # 检查是否在冷却期内
        if userName in cooldown_users and current_time < cooldown_users[userName]:
            remaining = int(cooldown_users[userName] - current_time)
            print(f"{userName} 还需等待 {remaining} 秒才能重新添加")
            return
        
        if userName not in activeList:
            activeList.append(userName)
            # 移除冷却记录并启用按钮
            if userName in cooldown_users:
                del cooldown_users[userName]
            button_map[userName].configure(state="normal")
        else:
            activeList.remove(userName)
            # 设置10秒冷却
            cooldown_end = current_time + 10
            cooldown_users[userName] = cooldown_end
            # 禁用按钮提示冷却状态
            button_map[userName].configure(state="disabled")
            # 10秒后自动重置冷却
            app.after(10000, lambda name=userName: reset_cooldown(name))
    elif typeOfMessage == "teacher":
        if len(activeList) > 0:
            activeList.pop(0)
    printbutton(activeList)

def reset_cooldown(userName):
    """重置用户冷却状态,恢复按钮可用"""
    if userName in cooldown_users:
        del cooldown_users[userName]
    button_map[userName].configure(state="normal")
    print(f"{userName} 冷却结束,可重新添加")

def whichButton(m):
    incomingMessage = m
    typeOfMessage, userName = incomingMessage.split(",")
    buttons(typeOfMessage, userName)

def printbutton(user_list):
    print("当前活跃列表:", user_list)

customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")

app = customtkinter.CTk()
app.geometry("400x240")

# 创建学生按钮并映射到button_map
button1 = customtkinter.CTkButton(app, text="hampus", command=lambda m="student, hampus": whichButton(m))
button1.pack(pady=5)
button_map["hampus"] = button1

# 创建老师按钮
button2 = customtkinter.CTkButton(app, text="teacher", command=lambda m="teacher, namn": whichButton(m))
button2.pack(pady=5)

# 创建第二个学生按钮(修正拼写错误syudent→student)
button3 = customtkinter.CTkButton(app, text="emma", command=lambda m="student, emma": whichButton(m))
button3.pack(pady=5)
button_map["emma"] = button3

app.mainloop()

代码说明

  • 非阻塞冷却:使用app.after(10000, ...)实现10秒后自动执行重置逻辑,不会阻塞主程序的事件循环
  • 状态可视化:移除名字时禁用对应按钮,冷却结束后自动启用,用户能直观感知冷却状态
  • 冷却判断:通过时间戳对比判断用户是否处于冷却期,避免误操作

内容的提问来源于stack exchange,提问作者kallberg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 16:04:56