当某些事件发生时,我需要将 a 的状态更改为Button
to DISABLED
。NORMAL
该按钮当前使用以下代码在 DISABLED 状态下创建:
self.x = Button(self.dialog, text="Download", state=DISABLED,
command=self.download).pack(side=LEFT)
我怎样才能将状态更改为NORMAL
?
您只需将state
按钮设置self.x
为normal
:
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()
None
self.x
Button(...)
self.x
self.x.pack()
我认为更改小部件选项的快速方法是使用该configure
方法。
在您的情况下,它看起来像这样:
self.x.configure(state=NORMAL)
这对我有用。我不确定为什么语法不同,但是尝试激活、非活动、停用、禁用等的每种组合都非常令人沮丧。在小写大写的引号中,在括号中的括号中的引号中等等。好吧,这里是出于某种原因,对我来说获胜的组合..与其他人不同?
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()