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

时间:2014-07-29 03:43:38

标签: python user-interface python-3.x tkinter

我正在使用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进行了测试(如果有帮助的话)。

1 个答案:

答案 0 :(得分:3)

您可以使用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的第二次调用是在启用按钮之前处理所有待处理事件(在您的情况下点击)。

BTW,它是normal状态,而不是enabled使按钮恢复正常状态。