2

I JUST started learning about Python, I recently read a tutorial about switching windows using tkinter. In the tutorial, the guy switched windows from inside the button in the __init__ using lambda, but I want to go through a function before switching windows. The function runs from inside Class WindowA and needs to call a function inside class GLApp. After it runs the function, if it is successful, it's supposed to open up WindowB.

I get this error if I try to call show_frame from inside the function in windowA.

--controller is not defined

Can someone please explain the thought process behind actually getting to call the show_frame function from inside the windowA function, I'd appreciate it a bunch!

class GLApp(tk.Tk):

    def __init__(self, *args, **kwargs):

            self.frames = {}

        for F in (WindowA, WindowB):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

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


class windowA(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self,parent)

        #window styling and function calling

        self.attempt_login = tk.Button(self,text="Login",command= self.function)

        def function(self):

             try:

                trysomething

             else:

                controller.show_frame(WindowB)
3kt
  • 2,543
  • 1
  • 17
  • 29
raltandi
  • 87
  • 2
  • 10
  • Traceback would be appreciated. What are the arguments where you call `windowA()`? (The [`cgitb`](https://docs.python.org/2/library/cgitb.html) library may be useful to show more information than a regular traceback if it's not obvious from your code. Contrary to the name, it's not just for CGI scripts.) – tripleee Jun 02 '16 at 04:08
  • The app opens up by default on windowA. In the tutorial, he switches through screens using _controller.show_frame(frameName)_ inside ___init___ through the use of a button – raltandi Jun 02 '16 at 04:11
  • We can see that from the code you posted, but `windowA` has an `__init__` function which expects a `controller` argument and you are saying that it is not defined, so it sounds like you are calling the initializer incorrectly. But again, without a proper traceback, the error message you posted isn't really useful for diagnosing this. Please [edit] your question to include more details. – tripleee Jun 02 '16 at 04:14
  • Oh, I see now that `frame = F(container, self)` is what calls `windowA` so that part seems to be fine. – tripleee Jun 02 '16 at 04:16
  • I'm terribly sorry for my English as it is not my native language, so excuse me if I'm not clear enough with my explanations. Someone posted an answer but they seem to have deleted the post. I just initialized app=GLapp() and then used app.show_frame(windowB) on the inside of the windowA function. I still want to understand why though! – raltandi Jun 02 '16 at 04:22
  • @VladG Would you consider undeleting your answer, please? – tripleee Jun 02 '16 at 04:23
  • VladG's answer is incorrect, it should probably stay deleted. – Bryan Oakley Jun 02 '16 at 12:00

1 Answers1

1

What the tutorial doesn't tell you is that you should save an instance of controller so that you can use it in every function of the page classes. To see the original code that was copied for the tutorial, see https://stackoverflow.com/a/7557028/7432

In it you'll see that one of the first things each page class does is save the controller. Once you do that you can use self.controller anywhere in a page class.

Also, in your code you're nesting function inside of __init__. There is simply no reason to do that in this case. Move the function out one level, and write it like this:

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

    def function(self):
         try:
            trysomething
         else:
            self.controller.show_frame(WindowB)
Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685