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
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
t1 = continuous_threading.PeriodicThread(0.1, readSerial)
frame2 = tk.Frame(root, bg='#80c1ff') #remove color later
frame2.place(relx=0, rely=0.1, relheight=1, relwidth=1, anchor='nw')
scrollbar = scrolledtext.ScrolledText(frame2)
scrollbar.place(relx=0, rely=0, relheight=0.9, relwidth=1, anchor='nw')
t1.start()
root.mainloop()
However, i was experiencing error when i was closing my application. You can read more about this here: Closing my tkinter serial app, gives me an exception
So user AST suggested, i should use the after()
function.
So i tried this:
I kept the function readSerial()
exactly the same. I removed all the lines that involved threads (t1
).
And finally this:
root.after(100, readSerial)
root.mainloop()
But this doesn't work as expected.
In my tkinter frame, only the first line of the serial is printed, then nothing else.
How can i make this work with after()
? What is the proper way?