0

Sorry for being so bad, I feel like my problem is very easy to solve, but I couldn't even by trying / searching the web !

I'm doing an app based with the SeaOfBTCmodel, consisting in a multiframe project. So far it is working nicely (even if I'm not 100% sure this is the best approach to achieve my goal), but I do have a problem : when I open a page, I want it to be reinitialized. But if I used it already and put some values in entry for example, those value would still be there when coming back to that same page.

I don't know how to reinitialize frames when making them reappear with tkraise. Althought, an other solution could be to destroy the frame when going to an other one from the menu.

Here is the most basic code I could go to in order to show the whole mechanism :

    import tkinter as tk


class SeaOfBTC(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)

        menubar = tk.Menu(container)
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label='StartPage', command=lambda: self.show_frame(StartPage))
        filemenu.add_separator()
        filemenu.add_command(label='PageOne', command=lambda: self.show_frame(PageOne))
        menubar.add_cascade(label='Pages', menu=filemenu)
        tk.Tk.config(self, menu=menubar)

        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 show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="StartPage")
        label.pack(pady=10, padx=10)


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text='PageOne')
        label.pack(pady=10, padx=10)

        my_entry = tk.Entry(self, textvariable='')
        my_entry.pack(padx=10, pady=10)


app = SeaOfBTC()
app.geometry("300x300")
app.mainloop()

I guess it could be solved in the show_frame function. I tried to use frame.__init__(), but I have an error message asking me for parent and controller arguments : I don't know what to use there, even after testing some things. If going for the .destroy(frame), it just doesn't open anymore.

Thank you in advance for your help on that question, and maybe your insight on the whole !

All the best,

Jacques
  • 11
  • 4

1 Answers1

1

I used the approach proposed by Stevoisiak. As it looks it may lead to memory problems, I completed using the gc module. After some testing, it looks all functional.

Although, I'm still not against an answer to my question, using the first approach, just for the sake of understanding/learning something !

Thank you everyone for reading,

Jacques
  • 11
  • 4