简体   繁体   中英

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; when released the button, function will no more be executed

I currently have a tk.Button(self, text="S+", command=sch_plus_callback) with

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. 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. So that the loop does not block the tkinter, you need to run it through after(time, callback). Tkinter loop link 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM