Miggy
Miggy

Reputation: 63

Tkinter: refresh frame/window from an outside function

I would like to refresh the MainPage when the updateCustomerList is finished updating the list so that this updated list is shown on the MainPage widgets.

I tried playing around with tk.show_frame(<frame>) and etc. but since the function itself isn't tied to the main Tkinter frame itself or isn't even a Tkinter object, then I'm not entirely sure how to reload the page. Any suggestions?

The code below is a snippet of my entire program:

customerList = [] #list is updated at the updateCustomerList function; global variable

class POS(tk.Tk):
    def __init__(self,*args,**kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        self.frames = {}
        for F in (ErrorPage, PaymentPage, MainPage): 
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column = 0, sticky = "nsew")

        #show frame here


class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        """
        Tkinter frame that I would like to "refresh" and use the new updated list
        """
        frame5Button = ttk.Button(frame5, text = "Add Item", command = lambda: updateCustomerList(barCode, quantity))
        frame5Button.grid(row = 0, column = 5, padx = 90, pady = 10)
        #This button allows me to go into the updateCustomerList function

def updateCustomerList(barCode, quantity):
    #some code to update a list
    #when function finishes updating the list, I would like to go back to the MainPage Tk frame and reload all the widgets like labels and entry boxes using the updated customerList list

app = POS()
app.geometry("700x700") 
app.resizable(False, False)
app.after(100, MasterFilePopUp)
app.mainloop()

Upvotes: 0

Views: 1775

Answers (1)

Nae
Nae

Reputation: 15335

Just remove and recreate the instance of Mainframe inside as the last line of updateCustomerList:

container = 0 #global variable

Add "global container" inside POS(tk.Tk)

def updateCustomerList(barCode, quantity):
    global app
    ...
    app.frames[MainPage].destroy()
    app.frames[MainPage] = MainPage(container, app)
    app.frames[MainPage].grid(row=0, column = 0, sticky = "nsew")
    app.frames[MainPage].tkraise() 
#function ends here

Upvotes: 2

Related Questions