Python Forum

Full Version: What is Controller in this code i am not understanding what is it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import tkinter as tk
from tkinter import ttk

LARGE_FONT = ("Verdana",12)



class SeaofBTCapp(tk.Tk):
   def __init__(self,*args,**kwargs):

       tk.Tk.__init__(self,*args,**kwargs)
       tk.Tk.wm_title(self,"BTC")
       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")
       self.show_frame(StartPage)

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



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(padx=10,pady=10)
       button = ttk.Button(self,text="Visit Page 1",
                           command=lambda:controller.show_frame(PageOne))
       button.pack()
       button = ttk.Button(self, text="Visit Page 2",
                          command=lambda: controller.show_frame(PageTwo))

       button.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(padx=10, pady=10)
       button = ttk.Button(self, text="Back to Home",
                           command=lambda: controller.show_frame(StartPage))

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

       button1.pack()


class PageTwo(tk.Frame):
   def __init__(self,parent,controller):
       tk.Frame.__init__(self,parent)
       label = tk.Label(self, text="Page Two", font=LARGE_FONT)
       label.pack(padx=10, pady=10)
       button = ttk.Button(self, text="Back to Home",
                           command=lambda: controller.show_frame(StartPage))

       button.pack()
       button1 = ttk.Button(self, text="Go to Page1",
                          command=lambda: controller.show_frame(PageOne))

       button1.pack()




app = SeaofBTCapp()
app.mainloop()
Do you have more code?
need to see where the class is used.

Quote:From stackoverflow:
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.
https://stackoverflow.com/questions/3286...inter-init
There sort of isn't one, but I guess SeaofBTCapp is close to a controller, and the other classes could be views. But this is really closer to just being a God Class that does everything and fills every role.