在其命令函数中引用 Tkinter 按钮时出现属性错误

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

我有一个 class,它是一个 Tkinter 框架,里面有一个 Tkinter 按钮。当我尝试在其 command 函数中更改按钮的 属性 时,出现错误 AttributeError: 'ButtonsFrame' object has no attribute 'solve_button'.

密码是:

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"

请帮我解决这个错误,因为我想 clear_button 也会遇到同样的问题。 谢谢!

命令应该是一个指向函数的指针

在您编写的代码中,命令从函数中获取 return 值。

command=self.solve_button_clicked()

正确的做法是

command=self.solve_button_clicked