Python/tkinter: Change label twice with one command execution
I suspect that the command refresh_table effects the target widgets once all demands are done.
That's true. Your GUI will update every time the mainloop
is entered. While your function refresh_table
is running, no events (like updating your label) will be processed, since those only occur during idle times (hence their other name, "idle tasks"). But because the UI isn't idle during the execution of refresh_table
, the event loop is not accessible until all code in the function is done (because your whole program runs on the same thread) and the redrawing event is pending. You can fix this by calling update_idletasks
, which will process all pending events immediately. Would be done like this (assuming that your main window is called parent
and you are inside a class definition, the important thing is that you call update_idletasks
on your main window directly after changin the label's text):
def refresh_table(self, args): label['text'] = 'Executing' self.parent.update_idletasks() print_data() label['text'] = 'Done'
There's also update
, which will not only call pending idle tasks, but rather process basically everything that there is to process, which is unnecessary in your case. A very nice explanation why update_idletasks
is mostly preferable to update
can be found in the answer to this question.