Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding parent and controller in Tkinter __init__

Tags:

python

tkinter

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?

like image 943
Vasanth Nag K V Avatar asked Sep 30 '15 11:09

Vasanth Nag K V


People also ask

What is parent and controller in Python?

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.

What is __ init __ In tkinter?

Basically, the __init__ method of a class is called as soon as an instance of that class is created. An example of the instance being created in your code is : app = Application(master=root) this means that your class must be called 'Application' though you haven't included that part.

What is controller in Python tkinter?

Controller. A controller acts as the intermediary between the views and models. The controller routes data between the views and models. For example, if users click the save button on the view, the controller routes the “save” action to the model to save the data into a database and notify the view to display a message ...

What does root Tk () do in tkinter?

It helps to display the root window and manages all the other components of the tkinter application. We can initialize the tkinter instance by assigning the variable to it.


1 Answers

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

like image 65
Bryan Oakley Avatar answered Nov 03 '22 20:11

Bryan Oakley