pythonwidgetscalingcustomtkinter

Trouble Resizing widgets in Custom Tkinter when going into another page


I was trying to use the set_widget_scale function to resize my widgets based on the resolution of the window, when I resize the widgets on the main page it works fine when the resolution is chosen, when I go to the other page it raises this error:

("_tkinter.TclError: invalid command name ".!ctkoptionmenu.!dropdownmenu"")
import customtkinter as ct

class App(ct.CTk):
    def __init__(self):
        super().__init__()
        self.title("Test")
        self.geometry("640x480")
        self.Page1()

    def Page1(self):
        for i in self.winfo_children():
            i.destroy()
        self.page1 = ct.CTkButton(self , text = "Page 2" , command = lambda: (self.Page2() , self.Resize()))
        self.page1.pack()
        self.menu = ct.CTkOptionMenu(self)
        self.menu.pack()


    def Page2(self):
        for i in self.winfo_children():
            i.destroy()
        self.page2 = ct.CTkButton(self , text = "Page 1" , command = lambda: (self.Page1() , self.Resize1()))
        self.page2.pack()
        self.menu = ct.CTkOptionMenu(self)
        self.menu.pack()

    def Resize(self):
        ct.set_widget_scaling(2)

    def Resize1(self):
        ct.set_widget_scaling(0.5)

if __name__ == "__main__":
    app = App()
    app.mainloop()

This is an example of my problem that I am facing


Solution

  • This method avoids using destroy constantly. Instead, it uses frames to separate pages. You can destroy the frames when not used, as demonstrated by the added destroy button.

    import customtkinter as ct
    
    
    class App(ct.CTk):
        def __init__(self):
            super().__init__()
            self.title("Test")
            self.geometry("640x480")
    
            self.end_button = ct.CTkButton(self, text="DESTROY")
            self.end_button.pack(padx=10, pady=30)
    
            self.page1_frame = ct.CTkFrame(self)
            self.page1_button = ct.CTkButton(self.page1_frame, text="Page 1", command=self.show_page2)
            self.page1_button.pack()
            self.page1_menu = ct.CTkOptionMenu(self.page1_frame)
            self.page1_menu.pack()
    
            self.page2_frame = ct.CTkFrame(self)
            self.page2_button = ct.CTkButton(self.page2_frame, text="Page 1", command=self.show_page1)
            self.page2_button.pack()
            self.page2_menu = ct.CTkOptionMenu(self.page2_frame)
            self.page2_menu.pack()
    
            self.page1_frame.pack()
    
            self.end_button.configure(command=lambda: (self.page1_frame.destroy(), self.page2_frame.destroy()))
    
        def show_page2(self):
            ct.set_widget_scaling(0.5)
            self.page1_frame.pack_forget()
            self.page2_frame.pack()
    
        def show_page1(self):
            ct.set_widget_scaling(2)
            self.page2_frame.pack_forget()
            self.page1_frame.pack()
    
    
    if __name__ == "__main__":
        app = App()
        app.mainloop()
    

    I combined the resize and page functions, but it works even when using

    command=lambda: (self.resize_small(), self.show_page2())

    hope this helps