0
import tkinter as tk
from PageTwoFile import PageTwoClass

class SeaofBTCapp(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")
        frame.grid(row=110, column=110, sticky="nsew")

    self.show_frame(StartPage)

def show_frame(self, cont):

    frame = self.frames[cont]
    frame.tkraise()

def qf(param):
  print(param)


class StartPage(tk.Frame):

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

    label = tk.Label(self, text="Start Page", font=LARGE_FONT)
    label.pack(pady=10,padx=10)

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

    button1 = tk.Button(self, text="Visit Page 2",
                        command=lambda: controller.show_frame(PageTwo))
    button1.pack()

class PageOne(tk.Frame):

def __init__(self, parent,controller):
    tk.Frame.__init__(self,parent)
    label = tk.Label(self, text="Page One", font=LARGE_FONT)
    label.pack(pady=10,padx=10)

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

    button2 = tk.Button(self, text="Two",
                        command=lambda: controller.show_frame(PageTwo))
    button2.pack()


 app = SeaofBTCapp()
 app.mainloop()

In this example I have a class similiar to PageOne but in another file.

class PageTwoClass(tk.Frame):

def __init__(self, parent,controller):
    tk.Frame.__init__(self,parent)
    label = tk.Label(self, text="Page Two", font=LARGE_FONT)
    label.pack(pady=10,padx=10)

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

    button2 = tk.Button(self, text="Page One",
                        command=lambda: controller.show_frame(PageOne))
    button2.pack()

I can run it fine, but when I go to PageTwo I can't go back to PageOne, I get:

NameError: name 'PageOne' is not defined

I assume that is going in to PageTwoFile and does not know how to come back. How to make it read everything?

I am working on a banking system and I have another files(customer,accounts) which are imported in the main file. If I want to change frames for when they are accessed I need them to go back...

  • What is the name of the file from which `PageTwo` needs to be imported? Where is that file, in the same directory? – Nae Jan 08 '18 at 22:52
  • 1
    Your indentation is wrong but the code seems to have, for the most part, nothing to do with the question you're asking anyway. – Nae Jan 08 '18 at 23:04
  • It should be `import the_py_file_in_the_same_directory_of_this_script_that_has_PageTwo as an_abbreviation` then use the `PageTwo` class in `an_abbreviation.PageTwo(parent, controller)` or `from the_py_file_in_the_same_directory_of_this_script_that_has_PageTwo import PageTwo` to be used like `PageTwo(parent, controller)`. – Nae Jan 08 '18 at 23:12
  • Hi, the file name is the same as the class, sorry forgot to specify that. –  Jan 09 '18 at 01:27

1 Answers1

2

The solution is to use the name of the class rather than the actual class itself, so that the different classes don't have to import each other.

A better version of the code you started from, with the modification to use the page name rather than the page class, is here: https://stackoverflow.com/a/7557028/7432

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685