TKINTER使用与参数的单个功能回调来选择该做什么

发布于 2025-01-28 05:05:12 字数 1709 浏览 2 评论 0原文

我编写了一个代码,该代码在保留按钮时会在循环中执行函数,并在释放按钮时停止执行。见下文。

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中的字符串(必须将Accoridngly更改为我要按下的按钮)我想知道是否有一种方法可以发送诸如self.sch_plus_button.bind('&lt; button; button; button; button-1&gt;',self 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:recursionError:recursionError:最大递归深度超过,所以我不知道问题如何解决

I've written a code that execute a function in loop while a button is hold and stop the execution when the button is released. See below.

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

Now, as soon as the sch_plus_callback function basically do always the same stuff, indipendently by the command I'm gonna press, and the only thing that changes is the string in ser.write (which have to change accoridngly to the button I'm pushing) I would like to know if there's a way to send an argument like self.sch_plus_button.bind('<Button-1>', self.sch_plus_callback("button_1")) and then get the argument inside my sch_plus_callback like

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

I've tried the code below but, because how my code is written, it triggers RecursionError: maximum recursion depth exceeded so I've no idea on how workaround the issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2025-02-04 05:05:12

我发现的一种解决方案

        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]将事件到Hendle

A solution I found has been

        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

Where args[0] is the argument that indicates the button pressed and args[1] the event to hendle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文