0

I want to modify the code from: Switch between two frames in tkinter. I put the three clases in three separates files but when I call master.switch_frame(master.StartPage) from pageOne.py that give the error : return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'StartPage'

Can someone help me with this error? I appreciate any suggestion.

the code is:

main.py

#take from: https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
# Multi-frame tkinter application v2.3
import tkinter as tk
import pageOne as p1
import pageTwo as p2

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is the start page").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Open page one",
                  command=lambda: master.switch_frame(p1.PageOne)).pack()
        tk.Button(self, text="Open page two",
                  command=lambda: master.switch_frame(p2.PageTwo)).pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

pageOne.py

# Multi-frame tkinter application v2.3
import tkinter as tk

class PageOne(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(master.StartPage)).pack()

if __name__ == "__main__":
    app = PageOne()
    app.mainloop()# Multi-frame tkinter application v2.3
import tkinter as tk

class PageOne(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(master.StartPage)).pack()

if __name__ == "__main__":
    app = PageOne()
    app.mainloop()

    enter code here

pageTwo.py

# Multi-frame tkinter application v2.3
import tkinter as tk

class PageTwo(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page two").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(master.StartPage)).pack()

if __name__ == "__main__":
    app = PageTwo()
    app.mainloop()

Thank you Bryan Oakley. finally the code that work is:

main.py

#take from: https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
# Multi-frame tkinter application v2.3
import tkinter as tk

from StartPage import StartPage
from pageOne import PageOne
from pageTwo import PageTwo

pages = {
    "StartPage": StartPage, 
    "PageOne": PageOne, 
    "PageTwo": PageTwo
}

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame("StartPage")

    def switch_frame(self, page_name):
        """Destroys current frame and replaces it with a new one."""
        cls = pages[page_name]
        new_frame = cls(master = self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

pageOne.py

# Multi-frame tkinter application v2.3
import tkinter as tk

class PageOne(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page", command=lambda: master.switch_frame("StartPage")).pack()

if __name__ == "__main__":
    app = PageOne()
    app.mainloop()

pageTwo.py

# Multi-frame tkinter application v2.3
import tkinter as tk

class PageTwo(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page two").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page", command=lambda: master.switch_frame("StartPage")).pack()

if __name__ == "__main__":
    app = PageTwo()
    app.mainloop()

StartPage.py

# Multi-frame tkinter application v2.3
import tkinter as tk

class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is the start page").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Open page one", command=lambda: master.switch_frame("PageOne")).pack()
        tk.Button(self, text="Open page two", command=lambda: master.switch_frame("PageTwo")).pack()
Alberto
  • 13
  • 3
  • what do You expect `master.StartPage` to be? You know right that You haven't defined anything like this because if You were then `SampleApp` would contain something like `self.StartPage = ...` – Matiiss Jun 15 '21 at 21:49

1 Answers1

1

I recommend passing a string as the argument to show_frame. That way, other files don't need to import all of the other classes. You can then create a mapping to be used by switch_frame. That file becomes the only file that needs to import the other pages.

It should look something like the following code. In this example I moved StartPage into a separate file for consistency:

from startPage import StartPage
from pageOne import PageOne
from pageTwo import PageTwo

pages = {
    "StartPage": StartPage, 
    "PageOne": PageOne, 
    "PageTwo": PageTwo
}

class SampleApp(tk.Tk):
    ...
    def switch_frame(self, page_name):
        """Destroys current frame and replaces it with a new one."""
        cls = pages[page_name]
        new_frame = cls(master=self)
        ...

Your other pages don't need to import the pages, they just need to use the page name:

class PageTwo(tk.Frame):
    def __init__(self, master):
        ...
        tk.Button(..., command=lambda: master.switch_frame("StartPage")).pack()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685