如何将Tkinter Button的状态从禁用更改为正常? [英] How to change Tkinter Button state from disabled to normal?
问题描述
发生某些事件时,我需要将状态从DISABLED
更改为Button
的NORMAL
.
I need to change the state from DISABLED
to NORMAL
of a Button
when some event occurs.
这是我的按钮的当前状态,当前已被禁用:
Here is the current state of my Button, which is currently disabled:
self.x = Button(self.dialog, text="Download",
state=DISABLED, command=self.download).pack(side=LEFT)
self.x(state=NORMAL) # this does not seem to work
Anyonne可以帮我吗?
Can anyonne help me on how to do that?
推荐答案
您只需要将按钮self.x
的state
设置为normal
:
You simply have to set the state
of the your button self.x
to normal
:
self.x['state'] = 'normal'
或
self.x.config(state="normal")
此代码将在事件的回调中使用,该事件将导致启用按钮.
This code would go in the callback for the event that will cause the Button to be enabled.
此外,正确的代码应为:
Also, the right code should be:
self.x = Button(self.dialog, text="Download", state=DISABLED, command=self.download)
self.x.pack(side=LEFT)
Button(...).pack()
中的方法pack
返回None
,并且您将其分配给self.x
.您实际上想将Button(...)
的返回值分配给self.x
,然后在下一行中使用self.x.pack()
.
The method pack
in Button(...).pack()
returns None
, and you are assigning it to self.x
. You actually want to assign the return value of Button(...)
to self.x
, and then, in the following line, use self.x.pack()
.
这篇关于如何将Tkinter Button的状态从禁用更改为正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!