gpt4 book ai didi

python - 防止按钮按下注册 TKInter/Python 3.3

转载 作者:行者123 更新时间:2023-11-28 16:36:50 24 4
gpt4 key购买 nike

我正在使用 Python 3.3/Tkinter 创建一个填充程序,遇到了一个真正让我难过的问题。基本上,我想防止在某些过程发生时注册按钮按下。我尝试将按钮的状态更改为 state='disabled' 但这只会将点击从注册延迟到函数完成运行之后。换句话说,尽管按钮被“禁用”,但如果单击它,按钮按下将在重新启用后立即注册。请参阅下面的示例代码。

def Button_Action:
Button.config(state='disabled')
#Activate some hardware that takes a few seconds.
Button.config(state='normal')

因此,问题是:如何在 Tkinter/Python 3 中有选择地忽略按钮按下?

我真的是 Python 的新手,并尝试搜索相关问题无济于事,所以如果这是一个愚蠢的问题,或者之前有人问过,请原谅我。此外,我已经使用 Radiobutton 和标准 Button 进行了测试(以防有帮助)。

最佳答案

您可以使用 update方法。

def button_action():
b.config(state='disabled')
b.update() # <----------
# Activate some hardware that takes a few seconds.
b.update() # <----------
b.config(state='normal')

第一次调用update 是为了使按钮显示为禁用状态。

update 的第二次调用是在启用按钮之前处理所有未决事件(在您的案例中是点击)。

顺便说一句,是正常状态,而不是enabled让按钮回到正常状态。

关于python - 防止按钮按下注册 TKInter/Python 3.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com