41

当某些事件发生时,我需要将 a 的状态更改为Buttonto DISABLEDNORMAL

该按钮当前使用以下代码在 DISABLED 状态下创建:

self.x = Button(self.dialog, text="Download", state=DISABLED,
                command=self.download).pack(side=LEFT)

我怎样才能将状态更改为NORMAL

4

3 回答 3

74

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

self.x['state'] = 'normal'

或者

self.x.config(state="normal")

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


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

self.x = Button(self.dialog, text="Download", state=DISABLED, command=self.download)
self.x.pack(side=LEFT)

返回中的方法pack,您将其分配给. 您实际上想将返回值分配给,然后在下一行中使用。Button(...).pack()Noneself.xButton(...)self.xself.x.pack()

于 2013-04-16T20:45:59.443 回答
8

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

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

self.x.configure(state=NORMAL)
于 2013-12-21T09:26:36.957 回答
0

这对我有用。我不确定为什么语法不同,但是尝试激活、非活动、停用、禁用等的每种组合都非常令人沮丧。在小写大写的引号中,在括号中的括号中的引号中等等。好吧,这里是出于某种原因,对我来说获胜的组合..与其他人不同?

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()
于 2019-10-24T04:33:39.310 回答