Python Forum
[Tkinter] What is Controller in this code i am not understanding what is it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] What is Controller in this code i am not understanding what is it
#1
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()
Reply
#2
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
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Not understanding the correlation between code and geometry with Tkninter Intelligent_Agent0 3 2,958 Aug-04-2018, 08:22 AM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020