Python 如何将 Tkinter 按钮状态从禁用更改为正常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16046743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Tkinter Button state from disabled to normal?
提问by scandalous
I need to change the state from DISABLEDto NORMALof a Buttonwhen some event occurs.
我需要状态从改变DISABLED到NORMAL的Button,当一些事件发生。
Here is the current state of my Button, which is currently disabled:
这是我的 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
Can anyonne help me on how to do that?
任何人都可以帮助我如何做到这一点?
采纳答案by Sheng
You simply have to set the stateof the your button self.xto normal:
您只需state将按钮的 设置self.x为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 packin 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().
该方法pack的Button(...).pack()回报None,且将其分配给self.x。实际上,你要分配的返回值Button(...)来self.x,然后在下面的行,使用self.x.pack()。
回答by guibe80
I think a quick way to change the options of a widget is using the configuremethod.
我认为更改小部件选项的快速方法是使用该configure方法。
In your case, it would look like this:
在你的情况下,它看起来像这样:
self.x.configure(state=NORMAL)
回答by DAVID STELMACK
This is what worked for me. I am not sure why the syntax is different, But it was extremely frustrating trying every combination of activate, inactive, deactivated, disabled, etc. In lower case upper case in quotes out of quotes in brackets out of brackets etc. Well, here's the winning combination for me, for some reason.. different than everyone else?
这对我有用。我不确定为什么语法不同,但是尝试激活、非活动、停用、禁用等的每一种组合都非常令人沮丧。在小写大写中引号中的引号中括号中的括号等。嗯,这里是出于某种原因,我的获胜组合与其他人不同?
import tkinter
class App(object):
def __init__(self):
self.tree = None
self._setup_widgets()
def _setup_widgets(self):
butts = tkinter.Button(text = "add line", state="disabled")
butts.grid()
def main():
root = tkinter.Tk()
app = App()
root.mainloop()
if __name__ == "__main__":
main()

