Indigo Indigo - 3 months ago 12
Python Question

Tkinter, why does my code skip the first label update?

I have a button that I want to press to process some excel data. This often takes a moment or two especially if the user uploads a number of large files.
While the files are being processed, I want to display a note to the user that the files are processing and that the program has not frozen. Once the files are complete, I want to indicate that as well.

This is the relevant code I have so far:

self.l2 = Label(self, text = " ", width = 20)
self.l2.grid(row = 3, column = 2, sticky = W)

# Calculate button
self.b2 = Button(self, text = "Calculate", command = self.calculate, width = 20)
self.b2.grid(row = 3, column = 3, sticky = W)


def calculate(self):
self.l2.config(text="Processing...")
get_data(filelist, self.v.get())
self.l2.config(text="Files Ready!")


The label is initially blank like I want and once the files are ready it shows "files ready" but it never seems to hit the "processing" label even if I upload enough files for the get_data command to take nearly a minute.

Why exactly would it not be showing up? It seems like the commands should execute sequentially, the label should update, get_data should be called, then once get _data has returned, the next label update should happen. Is this logic incorrect?

Answer

In order for the screen to refresh, the event loop must be able to process a refresh event. Since your code is running, the event loop is frozen.

A quick and dirty solution is to call self.update_idletasks(), which tells tkinter to handle all of the "idle" events, which includes events related to updating the screen.

self.l2.config(text="Processing...")
self.update_idletasks()
get_data(filelist, self.v.get())
self.l2.config(text="Files Ready!")
Comments