Running a while loop in a method of a tkinter UI class while still allowing the UI to operator Running a while loop in a method of a tkinter UI class while still allowing the UI to operator tkinter tkinter

Running a while loop in a method of a tkinter UI class while still allowing the UI to operator


Tkinter provides an after method that allows you to schedule a command to be run after some number of milliseconds. In the after handler you can check your state, make appropriate modifications and schedule another call to the same handler. This was you never block the running UI thread but arrange for a regular call to a method.

The after method returns a token that can be used to cancel the scheduled task. So provided you retain that token you can pass this to the after_cancel method to cancel an outstanding scheduled call.