Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting an attribute error when referencing a Tkinter Button in its command function

I have a class which is a Tkinter Frame, within it is a Tkinter Button. When I try to change a property of the button within its command function, I get the error AttributeError: 'ButtonsFrame' object has no attribute 'solve_button'.

The code is:

class ButtonsFrame(tk.Frame):
    def __init__(self, container, puzzle_grid, options_bottom_frame):
        super().__init__(container)

        self.solve_button = tk.Button(self, text="Solve", font=(20), command=self.solve_button_clicked())
        self.solve_button.grid(column=0, row=0, padx=5)

        self.clear_button = tk.Button(self, text="Clear", font=(20), command=self.clear_button_clicked())
        self.clear_button.grid(column=1, row=0, padx=5)
        self.clear_button["state"] = "disabled"

        self.grid(row=2, sticky="W", pady=5)

    def solve_button_clicked(self):
        
        # some deleted irrelevant code

        if is_valid:
            
            # some more irrelevant deleted code

            self.solve_button["state"] = "disabled"
            self.clear_button["state"] = "normal"


    def clear_button_clicked(self):
        reset_cell_text(cell_texts)
        solve_button["state"] = "normal"
        clear_button["state"] = "disabled"

Please help me with this error, as I imagine I’ll have the same problem with clear_button.
Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The command should be a pointer to a function

In the code you wrote, the command gets the return value from the function.

command=self.solve_button_clicked()

The correct way is

command=self.solve_button_clicked
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading