1

I have seen many explanations of how to turn an enabled button disabled but not when classes are involved. The error here is in the line 'button_1.config...' and the error message is that button_1 is not defined. I think this is because it is in a different method but im not sure how to disable a button from a different method. any help is appreciated.

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()

1 Answer 1

1

The button needs to be an instance variable, if you're accessing it between methods in the class. Just add self. in front of it. It's also going to need to be packed on a separate line, otherwise the instance variable self.button_1 will return 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.