Python Tkinter,如何禁用类中的按钮?
我已经看到了很多关于如何禁用启用按钮的解释,但是当涉及到类时就没有了。这里的错误在“button_1.config…”行中,错误消息是button_1未定义。我认为这是因为它是在一个不同的方法,但我不知道如何禁用一个按钮从一个不同的方法。感谢您的帮助Python Tkinter,如何禁用类中的按钮?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我已经看到了很多关于如何禁用启用按钮的解释,但是当涉及到类时就没有了。这里的错误在“button_1.config…”行中,错误消息是button_1未定义。我认为这是因为它是在一个不同的方法,但我不知道如何禁用一个按钮从一个不同的方法。感谢您的帮助 from tkinter import * class menu: def __init__(self, master): self.master = master button_1 = Button(se
from tkinter import *
class menu:
def __init__(self, master):
self.master = master
button_1 = Button(self.master, text = 'test', command = self.correct).pack()
def correct(self):
button_1.config(state = DISABLED)
def window():
root = Tk()
menu(root)
root.mainloop()
if __name__ == '__main__':
window()
如果要在类中的方法之间访问该按钮,则该按钮必须是实例变量。只需在它前面添加
self.
。它还需要在单独的一行中进行打包
,否则实例变量self.button_1
将返回None:
class menu:
def __init__(self, master):
self.master = master
self.button_1 = Button(self.master, text = 'test', command = self.correct)
self.button_1.pack()
def correct(self):
self.button_1.config(state = DISABLED)