TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.52k stars 1.08k forks source link

How to integrate frames and controllers with CTK? #2253

Open jacobranz opened 9 months ago

jacobranz commented 9 months ago

Hello, I currently have a project setup using many different frames and a controller class in order to switch between the different frames. This implementation is setup using tkinter and not customtkinter. I am working now to get this moved over to customtkinter. But keep running into issues when I go to initialize my frame. I have attached the error code as well as a code snippet of the actual controller class. Any help would be GREATLY appreciated! I am annoyed at how long this has taken me as is.

Error when initializing frame: AttributeError: 'ReportCard' object has no attribute 'tk'

Code Snippet: `

class PageContainer(ctk.CTkFrame):

def __init__(self, master, **kwargs):
    super().__init__(master, **kwargs)

    self.container = container = ctk.CTkFrame(self)
    container.pack(side='top', fill='both', expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    self.frame = {}

    for F in (Example, ReportCard, Math150, English120, Music100, Physics101, ClassPage, EnterGrades_English120, EnterGrades_Math150, EnterGrades_Music100, EnterGrades_Physics101, AddStudent_English120, AddStudent_Math150, AddStudent_Music100, AddStudent_Physics101):
        frame = F(container, self)
        self.frame[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame(Example)

def show_frame(self, controller):
    if controller not in self.frame:
        self.frame[controller] = frame = controller(self.container, self)
        frame.grid(row=0, column=0, sticky="nsew")
    frame = self.frame[controller]
    frame.CTkraise()

def get_page(self, page_class):
    return self.frame[page_class]`
ghost commented 9 months ago

In this line: frame = F(container, self), you might need to pass the self first. Like this: `frame = F(self, container)