如何将Tkinter Button状态从禁用更改为正常?

sca*_*ous 33 python state tkinter button

我需要状态从改变DISABLEDNORMALButton,当一些事件发生.

这是我的Button的当前状态,目前已禁用:

  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
Run Code Online (Sandbox Code Playgroud)

anyonne可以帮助我如何做到这一点?

She*_*eng 60

您只需state将按钮设置self.xnormal:

self.x['state'] = 'normal'
Run Code Online (Sandbox Code Playgroud)

要么

self.x.config(state="normal")
Run Code Online (Sandbox Code Playgroud)

此代码将进入将导致启用Button的事件的回调.


此外,正确的代码应该是:

self.x = Button(self.dialog, text="Download", state=DISABLED, command=self.download)
self.x.pack(side=LEFT)
Run Code Online (Sandbox Code Playgroud)

该方法packButton(...).pack()回报None,且将其分配给self.x.实际上,你要分配的返回值Button(...)self.x,然后在下面的行,使用self.x.pack().

  • 谢谢盛工作:)顺便说一句,我正在使用python 2.7所以self.x ['state'] ='主动'只是为了可能遇到同样情况的其他人 (3认同)

小智 7

我认为更改窗口小部件选项的快速方法是使用该configure方法.

在你的情况下,它看起来像这样:

self.x.configure(state=NORMAL)
Run Code Online (Sandbox Code Playgroud)