Tkinter load screen with updating labels

I am writing an application which involves a fair amount of data munging at launch. What I'd like to do is create a splash screen that tells the user what stage of the data loading process is happening in real time.

My plan was to create a Label and pass new text to that Label depending on what calculations were going on in that moment. However in my various attempts the best I've done is get the labels to show up only after the munging is complete.

I saw this, which helped me a bit, but still not getting all the way there: Tkinter Show splash screen and hide main screen until __init__ has finished

Below is my current best attempt (taking all the actual dataframe stuff out to make it minimally executable)

[EDIT TO ADD] Ideally I'd like to do this in a way that doesn't require all the data munging to occur inside the class. IOW, phase 1 launches the splash screen, phase 2 runs the data munging in the main code, phase 3 launches the primary UI

import time
from tkinter import *

class LoadScreen(Toplevel):
def init(self, parent):
Toplevel.init(self, parent)
self.title(‘Loading’)
self.update()

class UserInterface(Tk):
def init(self, parent):
Tk.init(self, parent)
self.parent=parent
self.withdraw()
loader = LoadScreen(self)
self.load_label = Label(loader, text=‘Loading’)
self.load_label.grid(row=0, column=0, padx=20, pady=20)
self.stage_label = Label(loader, text=‘Preparing dataframe’)
self.stage_label.grid(row=1, column=0, padx=20, pady=20)
#loader.destroy()
self.main_screen()

def main_screen(self):
    self.deiconify()
    self.load_label = Label(self, text='Done')
    self.load_label.grid(row=0, column=0, padx=20, pady=20)
    self.close_button = Button(self, text='Close', 
        command = lambda: self.destroy())
    self.close_button.grid(row=1, column=0, padx=20, pady=20)

ui = UserInterface(None)

#Pretend I’m doing some dataframe munging
print(‘1’)
time.sleep(2)
ui.stage_label[‘text’] = ‘Running some calculations’
print(‘2’)
time.sleep(2)
ui.stage_label[‘text’] = ‘Finishing up’
print(‘3’)
time.sleep(2)

ui.mainloop()


#python

1 Likes58.70 GEEK