Skip to content
Advertisement

How can I refresh/reinitialize a class with the values gotten from another class?

So the question title pretty much sums it up. I apologise in advance if I have too much information as I don’t know exactly where the problem arises but the issue is in the transition between class StartPage and class PageOne.I basically want to get the text from first_button (I will add more of these later) and grab data from a file which has a name matching to the button text. This data will then be added in the for-loop in PageOne. However, it seems like PageOne is at its initial state despite the function transition()

from tempSimulator import lasdata
import tkinter as tk

class SalladBar:

    def __init__(self,master):
        self.master=master
        master.geometry("1300x700")
        container = tk.Frame(self.master)

        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 (startPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(startPage)

    def update_frame(self, page):
        frame = self.frames[page]
        frame.update()

    def show_frame(self, page):
        frame = self.frames[page]
        frame.tkraise()

class variables:
    data1=[]
    data2=[]

class startPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        f1 = tk.Frame(self, width="1300", height="200", bd=6, relief="raise")
        f1.pack()
        label = tk.Label(f1, text="xxxx")
        label.pack(pady=10, padx=10)

        #Add more buttons
        first_button= tk.Button(f1, text="Source",
                            command=lambda: self.transition(controller,first_button.cget("text")))
        first_button.pack()

    def transition(self,controller,source):

        data1,data2=lasdata(source)
        variables.data1=data1
        variables.data2=data2
        controller.update_frame(PageOne) #I thought this would do the trick
        controller.show_frame(PageOne)


class PageOne(tk.Frame):
    print("PageOne initialised")

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        f1=tk.Frame(self, width="1300", height="200", bd=6, relief="raise")
        f1.pack()
        label = tk.Label(f1, text="Title2")
        label.pack(pady=10, padx=10)

        for d in variables.data1:
            sld_label= tk.Label(f1, text= d.givename() + "n" + d.givedet())
            sld_label.pack()

        button1 = tk.Button(self, text="Back to Home",
                                 command=lambda: controller.show_frame(startPage))
        button1.pack()

root = tk.Tk()
app = SalladBar(root)
root.mainloop()

Advertisement

Answer

You need to move the data adding code to a separate method if you want to call it later. Things that are in the __init__ method are executed when the instance is created, and never again.

class PageOne(tk.Frame):
    print("PageOne initialised")
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.f1=tk.Frame(self, width="1300", height="200", bd=6, relief="raise")
        self.f1.pack()
        label = tk.Label(self.f1, text="Title2")
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="Back to Home",
                                 command=lambda: controller.show_frame(startPage))
        button1.pack()
    
    def add_data(self):
        for d in variables.data1:
            sld_label= tk.Label(self.f1, text= d.givename() + "n" + d.givedet())
            sld_label.pack()

Once you have done that you can call that method from another class like this:

def transition(self,controller,source):
    data1,data2=lasdata(source)
    variables.data1=data1
    variables.data2=data2
    controller.frames[PageOne].add_data() # update the other page
    controller.show_frame(PageOne)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement