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
Can anyonne help me on how to do that?
解决方案
You simply have to set the state of the your button self.x to normal:
self.x['state'] = 'normal'
or
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)
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中按钮状态改变问题展开。用户想在事件发生时将按钮从禁用状态改为正常状态,原代码尝试修改未成功。解决方案是使用self.x['state'] = 'normal'或self.x.config(state='normal'),同时指出原代码赋值错误,应先将Button返回值赋给self.x,再调用pack方法。
1249

被折叠的 条评论
为什么被折叠?



