I am using tkinter to display some labels based on voltages I am reading. However, it stops executing after one read. I have found that it is due to root.mainloop(). But I a
Tkinter needs mainloop
to work (to call all functions behind).
Use root.after(time_ms, function_name_without_() )
(before mainloop
)
to run some function - and that function have to run the same after(...)
to work in loop.
In Tkinter you don't use while True
because while True
is to make (main)loop
but Tkinter has own mainloop
.
And don't use sleep()
- use after()
Put all code from while True
(except mainloop
) in some function and call it using after()
.
Use second after()
in place of sleep()
.
import Tkinter as tk
master = tk.Tk()
def my_mainloop():
print "Hello World!"
master.after(1000, my_mainloop)
master.after(1000, my_mainloop)
master.mainloop()
An alternative solution I found to work better - just start your while loop in another thread, and use global variables. My code worked perfectly doing it that way.