-1

I have tried googling and everyone seems to have structured their code completely differently. I understand the base level tkinter, however I do not understand how people are using classes and def's to swap pages. How can I swap from my main window to my second one? (and not open the second one after the main is closed)

import tkinter as tk
main = tk.Tk()
main.title("Main Program")
firstlabel = tk.Label(main, text="This is a program!")
firstlabel.pack()
main.mainloop()
second = tk.Tk()
second.title("Second Program")
firstlabel = tk.Label(second, text="This is another program!")
firstlabel.pack()
second.mainloop()

EDIT: (solution)

import tkinter as tk


class Application(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 (StartPage, PageOne, PageTwo):
            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="Start Page", font=("Consolas", 30))
        label.pack(pady=10,padx=10)

        button = tk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne))
        button.pack()


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

        label = tk.Label(self, text="Page One!!!", font=("Consolas", 30))
        label.pack(pady=10,padx=10)

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


app = Application()
app.mainloop()
  • Does this answer your question? [Python: Can I open two Tkinter Windows at the same time?](https://stackoverflow.com/questions/43552320/python-can-i-open-two-tkinter-windows-at-the-same-time) – Nishant Agarwal Feb 28 '20 at 17:33
  • 1
    Generally speaking, you shouldn't call `tk.Tk()` twice. Even though it's a different structure, I suggest you look at [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter). – martineau Feb 28 '20 at 18:07
  • Does this answer your question? [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) – AMC Feb 28 '20 at 20:42

2 Answers2

0

Try this::::

tk.Toplevel(main)

NaSir HuSSaiN
  • 192
  • 4
  • 12
0

One way do this is by clearing everything (every widget), with this function:

def clear(app):
    # Delete everything else in app
    widget_list = app.winfo_children()
    for item in widget_list:
        if item.winfo_children():
            widget_list.extend(item.winfo_children())
    for item in widget_list:
        item.pack_forget()

And then puting in the new window you want to swap to (every window should hav its own frame to make it simpler).

DYD
  • 382
  • 2
  • 15
  • You don't need to call `pack_forget` in all of the child windows. Just remove the widgets in `app.winfo_children()` and all of the children will by definition be invisible since their parent is invisible. – Bryan Oakley Feb 28 '20 at 17:59