tkinter 使用带参数的单函数回调来选择要做什么

tkinter uses single function callback with arguments to chose what to do

我编写了一段代码,在按住按钮时循环执行函数,并在释放按钮时停止执行。见下文。

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        
        self.sch_plus_button = tk.Button(self, text="S+", command=self.sch_plus_callback)
        self.sch_plus_button.pack()
        self.sch_plus_button.bind('<Button-1>', self.sch_plus_callback)
        self.sch_plus_button.bind('<ButtonRelease-1>', self.button_stop_callback)

    def button_stop_callback(self, event):
        self.after_cancel(repeat)

    def sch_plus_callback(self, *args):
        global repeat
        try:
            # TODO find right time to send data to keep RELE ON
            repeat = self.after(200, self.sch_plus_callback, args[0])
        except IndexError:
            pass
        self.ser.write(b'\x02\x56\x81\x80\x80\x80\x80\x80\x39\x35\x04')

现在,一旦 sch_plus_callback 函数基本上总是做同样的事情,独立于我要按下的命令,唯一改变的是 ser.write 中的字符串(必须根据我按下的按钮进行相应更改)我想知道是否有一种方法可以发送 self.sch_plus_button.bind('<Button-1>', self.sch_plus_callback("button_1")) 之类的参数,然后在我的 sch_plus_callback 中获取参数,例如

def sch_plus_callback(self, *args):
            global repeat
            try:
                # TODO find right time to send data to keep RELE ON
                repeat = self.after(200, self.sch_plus_callback(args[0]), args[1])
            except IndexError:
                pass
            self.ser.write(string_to_send(args[0]))

我已经尝试了下面的代码,但是由于我的代码是如何编写的,它会触发 RecursionError: maximum recursion depth exceeded 所以我不知道如何解决这个问题

我找到的解决方案是

        self.sch_plus_button = tk.Button(self, text="S+", command=lambda: self.sch_plus_callback('sch_plus'))
        self.sch_plus_button.pack()
        self.sch_plus_button.bind('<Button-1>', lambda event: self.sch_plus_callback('sch_plus', event))
        self.sch_plus_button.bind('<ButtonRelease-1>', self.button_stop_callback)

    def button_stop_callback(self, event):
        self.after_cancel(repeat)

    def sch_plus_callback(self, *args):
        global repeat
        logging.info("args are " + str(args))

        try:
            repeat = self.after(300, self.sch_plus_callback, args[0], args[1])
        except IndexError:
            pass

其中 args[0] 是指示按下按钮的参数,args[1] 是要处理的事件