TKINTER使用与参数的单个功能回调来选择该做什么
我编写了一个代码,该代码在保留按钮时会在循环中执行函数,并在释放按钮时停止执行。见下文。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现的一种解决方案
是
args [0]
是指示按钮的参数,args [1]
将事件到HendleA solution I found has been
Where
args[0]
is the argument that indicates the button pressed andargs[1]
the event to hendle