-1

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!

41d3n
  • 1
  • 2

1 Answers1

0

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