[英]tkinter executes function while the button stays pressed and stop when released
I'm new to tkinter and I'm looking for a button that executes a function in loop as soon as the button is pressed;我是 tkinter 的新手,我正在寻找一个在按下按钮后立即循环执行功能的按钮; when released the button, function will no more be executed松开按钮后,功能将不再执行
I currently have a tk.Button(self, text="S+", command=sch_plus_callback)
with我目前有一个tk.Button(self, text="S+", command=sch_plus_callback)
def sch_plus_callback():
self.ser.write(b'\x02\x56\x81\x80\x80\x80\x80\x80\x39\x35\x04')
Now I would like some sort of button that does现在我想要某种按钮
def sch_plus_callback():
while button_is_pressed:
self.ser.write(b'\x02\x56\x81\x80\x80\x80\x80\x80\x39\x35\x04')
Is there a way?有办法吗?
My full code is我的完整代码是
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# self.insert_lengh_text_label = tk.Label(self, text="Max Packet Length")
self.sch_plus_button = tk.Button(self, text="S+", command=self.sch_plus_callback)
self.sch_plus_button.pack()
def sch_plus_callback(self):
self.ser.write(b'\x02\x56\x81\x80\x80\x80\x80\x80\x39\x35\x04')
I'm now using these methods but it's more a workaround then an actual solution我现在正在使用这些方法,但它更像是一种解决方法而不是实际的解决方案
def sch_plus_stop_callback(self, event):
self.after_cancel(repeat)
def sch_plus_callback(self, *args,):
global repeat
try:
repeat = self.after(300, self.sch_plus_callback, args[0])
except:
pass
self.ser.reset_input_buffer()
self.ser.reset_output_buffer()
self.ser.write(b'\x02\x56\x81\x80\x80\x80\x80\x80\x39\x35\x04')
In the function, catch the event of releasing the left mouse button "ButtonRelease-1",using the method widget.bind(event, handler), by which you interrupt the loop.在函数中,使用方法 widget.bind(event, handler) 捕获释放鼠标左键“ButtonRelease-1”的事件,通过该方法中断循环。 https://python-course.eu/tkinter/events-and-binds-in-tkinter.php#:~:text=Events%20can%20be%20key%20presses,and%20methods%20to%20an%20event.&text=If%20the%20defined%20event%20occurs,called%20with%20an%20event%20object. https://python-course.eu/tkinter/events-and-binds-in-tkinter.php#:~:text=Events%20can%20be%20key%20presses,and%20methods%20to%20an%20event.&text =如果%20the%20defined%20event%20发生,调用%20with%20an%20event%20object。 So that the loop does not block the tkinter, you need to run it through after(time, callback).为了使循环不会阻塞 tkinter,您需要通过 after(time, callback) 运行它。 Tkinter loop link https://tkdocs.com/tutorial/eventloop.html . Tkinter 循环链接https://tkdocs.com/tutorial/eventloop.html 。 Code as a sample:代码作为示例:
from tkinter import *
def sch_plus_callback(event):
global repeat
repeat = root.after(500, sch_plus_callback, event)
text.insert(END, "Bye Bye.....\n")
def stop_callback(event):
root.after_cancel(repeat)
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....\n")
text.pack()
button = Button(root, text="S+")
button.pack()
button.bind('<Button-1>', sch_plus_callback)
button.bind('<ButtonRelease-1>', stop_callback)
root.mainloop()
def sch_plus_stop_callback(self, event):
self.after_cancel(self.repeat)
def sch_plus_callback(self, event):
try:
self.repeat = self.after(300, self.sch_plus_callback, event)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.