Skip to content Skip to sidebar Skip to footer

Tkinter: Make Function Run Periodically With After()

I was trying to make a function run periodically. The purpose was to print serial data on a tkinter frame. Initially this worked, using threads. def readSerial(): global val1

Solution 1:

You have to use after() inside the function so as to call it periodically, like:

defreadSerial():
    global val1
    ser_bytes = ser.readline()
    ser_bytes = ser_bytes.decode("utf-8")
    val1 = ser_bytes
    scrollbar.insert("end", val1)
    scrollbar.see("end") #autoscroll to the end of the scrollbar
    root.after(100,readSerial) # 100 ms is 0.1 second, you can change that

.... # Same code but remove the t1

readSerial()
root.mainloop()

This will keep on repeating the function roughly every 100 milliseconds, there is no guarantee to call the function exactly at 100 millisecond, but it wont be called before 100 millisecond.

Post a Comment for "Tkinter: Make Function Run Periodically With After()"