9

I want to understand what the below code means:

class PageOne(tk.Frame):

    def __init__(self, parent, controller):

What are self, parent and controller? What is the role and scope of these tools here?

I believe self is similar to this in Java, but what's the use of parent and controller?

Later in the code flow I can see:

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

There is a function already defined called show_frame, but why is the controller being used to call this function?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Vasanth Nag K V
  • 4,860
  • 5
  • 24
  • 48
  • 1
    They are parameters to the method. What is confusing you about them? There is nothing special about `__init__` here, except that it is called automatically using the parameters passed to the instantiation call. – Daniel Roseman Sep 30 '15 at 11:17
  • 1
    *"I believe self is similar to this in Java"* - yes. *"what's the use of parent and controller?"* - they're just two other initialisation parameters. In Tkinter, you generally pass the `parent` widget within which the new widget sits to each new widget to define a tree for the whole UI. `controller` is apparently something that can be used to control the overall UI, rather than make a widget responsible for global changes. – jonrsharpe Sep 30 '15 at 11:18
  • Code appears to be from http://stackoverflow.com/q/29444725/3001761 – jonrsharpe Sep 30 '15 at 11:20
  • 4
    @jonrsharpe: actually, the original code came from this answer of mine from 2011: http://stackoverflow.com/a/7557028/7432. The question you link to refers to a tutorial on the internet which seems to have copied that answer almost verbatim -- and without attribution :-( – Bryan Oakley Sep 30 '15 at 11:53
  • I think it's worth pointing out that the `parent` and `controller` arguments have nothing intrinsically to do with `tkinter`, their meaning is entirely defined by the context in which they're being used (i.e. what the rest of the code in class is). – martineau Apr 11 '21 at 10:16

1 Answers1

31

Roughly speaking, the original code1 attempted to use a pseudo-MVC (model, view and controller) architecture. Though without the "model" part -- there was just a "view" (some frames) and a "controller" (the main application). Hence, the reference to a controller object. The original code was actually written to show how to "stack" frames, so it's MVC implementation is very shallow and under-documented since that wasn't the point of the example.

To answer your specific questions:

self represents the current object. This is a common first parameter for any method of a class. As you suggested, it's similar to Java's this.

parent represents a widget to act as the parent of the current object. All widgets in tkinter except the root window require a parent (sometimes also called a master)

controller represents some other object that is designed to act as a common point of interaction for several pages of widgets. It is an attempt to decouple the pages. That is to say, each page doesn't need to know about the other pages. If it wants to interact with another page, such as causing it to be visible, it can ask the controller to make it visible.

You asked "There is a function already defined called show_frame, but why is the controller being used to call this function?" Notice that show_frame is defined in a separate class, in this case the main program class. It is not defined in the other classes. For the other classes to be able to call it, they must call it on an instance of the main class. That instance is named controller in the context of these other classes.


1 Note: even though you likely found the original code in an online tutorial, it originally came from this stackoverflow answer: Switch between two frames in tkinter

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    just one question, so what ever classes are initialized at the Main Loop of a tkinter program, throws a "controller" to the class being initialized?? so this is the controller which is caught and initialized at __INIT__???? – Vasanth Nag K V Sep 30 '15 at 12:15
  • 2
    @VasanthNagKV: "throw" is an odd choice of words. But yes, the main program is being passed to each object as the "controller" parameter. – Bryan Oakley Sep 30 '15 at 12:17