2
class StartPage(tk.Frame):   
    def __init__(self, parent, controller):  
        tk.Frame.__init__(self, parent)
        self.controller = controller
        tk.Label(self, text='EPL Predictions').grid(row=0) 
        
        NG = tk.Button(self, text='New Game',width = 15, command=lambda: controller.show_frame(SecondPage)) 
        NG.grid(row=1)
        
        Upcoming = tk.Button(self, text='Upcoming Fixtures', width = 15, command=lambda: controller.show_frame(FirstPage)) 
        Upcoming.grid(row=2)
        
        OP = tk.Button(self, text='Odds Progression', width = 15, command=lambda: controller.show_frame(FourthPage)) 
        OP.grid(row=3)
        
        refresh = tk.Button(self, text='Refresh', width = 15, command=lambda: self.refresh_data()) 
        refresh.grid(row=4)




    def update_time(self,time_elapsed):
        
        //code to insert time elapsed to temporary Label

    def refresh_data(self):
        check_time = time.time()
        for i in range(10):
            time_elapsed = time.time()-check_time
            self.update_time(time_elapsed)
            //rest of the code to refresh data


So this is part of the code. What I am hoping to achieve is updating a label while the for loop under refresh_data is running (to show the progress of the function).

Right now the label only updates after the for loop is executed.

Shyam Nair
  • 48
  • 5

1 Answers1

1

You can use <any tkinter object>.update() or <any tkinter object>.update_idletasks(). Both of them update the window/widget and show all changes to the screen. I prefer .update() but if you want to know more about the differences between them look at this.

So in your case try this:

class StartPage(tk.Frame):   
    def __init__(self, parent, controller):  
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.label = tk.Label(self, text='EPL Predictions')
        self.label.grid(row=0) 
        
        NG = tk.Button(self, text='New Game',width = 15, command=lambda: controller.show_frame(SecondPage)) 
        NG.grid(row=1)
        
        Upcoming = tk.Button(self, text='Upcoming Fixtures', width = 15, command=lambda: controller.show_frame(FirstPage)) 
        Upcoming.grid(row=2)
        
        OP = tk.Button(self, text='Odds Progression', width = 15, command=lambda: controller.show_frame(FourthPage)) 
        OP.grid(row=3)
        
        refresh = tk.Button(self, text='Refresh', width = 15, command=lambda: self.refresh_data()) 
        refresh.grid(row=4)

    def update_time(self, time_elapsed):
        self.label.config("text"=time_elapsed)

    def refresh_data(self):
        check_time = time.time()
        for i in range(10):
            time_elapsed = time.time()-check_time
            self.update_time(time_elapsed)
            self.label.update()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31