Search code examples
pythontkintercustomtkinter

Python, gui, ctk


so, I am creating a blog, and right now, I am in the settings part, and I have a slide panel with a button that I want it to go to a new window where I will create new widgets and etc..., I have already done this kinda of thing to login and sign_up, the thing is that now I can't use pack.forget(), it just doesn't work

class SlidePanel(customtkinter.CTkFrame):
    def __init__(self, parent, start_pos, end_pos):
        super().__init__(master=parent, fg_color="white")

        # Attributes
        self.start_pos = start_pos
        self.end_pos = end_pos
        self.width = abs(start_pos - end_pos)

        # Animated attributes
        self.pos = start_pos
        self.in_start_pos = True

        # Layout
        self.place(relx=self.start_pos, rely=0.05, relwidth=0.25, relheight=0.9)  # Adjust the width to 0.25

        # Add buttons
        self.add_buttons()

    def add_buttons(self):
        button_explore = customtkinter.CTkButton(self, text="Explore", command=self.on_explore)
        button_profile = customtkinter.CTkButton(self, text="Profile", command=self.on_profile)
        button_settings = customtkinter.CTkButton(self, text="Settings", command=self.on_settings)

        button_explore.pack(expand=True, pady=7)
        button_profile.pack(expand=True, pady=7)
        button_settings.pack(expand=True, pady=7)

    def on_explore(self):
        print("Explore button clicked")

    def on_profile(self):
        print("Profile button clicked")

    def on_settings(self):
        open_settings_window()

    def animate(self):
        if self.in_start_pos:
            self.animate_forward()
        else:
            self.animate_backward()

    def animate_forward(self):
        if self.pos < self.end_pos:
            self.pos += 0.005  # Increase the step size for faster animation
            self.place(relx=self.pos, rely=0.05, relwidth=0.25, relheight=0.9)  # Adjust the width to 0.25
            self.after(5, self.animate_forward)
        else:
            self.in_start_pos = False

    def animate_backward(self):
        if self.pos > self.start_pos:
            self.pos -= 0.005  # Increase the step size for faster animation
            self.place(relx=self.pos, rely=0.05, relwidth=0.25, relheight=0.9)  # Adjust the width to 0.25
            self.after(5, self.animate_backward)
        else:
            self.in_start_pos = True

def open_settings_window():
    for widget in window.winfo_children():
        widget.pack_forget()

    window.title("Settings Hub")  # Update title

    # Create the new widgets for the settings page
    label = customtkinter.CTkLabel(window, text="test",)
    label.pack(pady=10)

I was expecting it to kinda refresh we will say and to put me a clean window, with new title, a label inside just for test


Solution

  • If you will recreate those widgets when switching page, then you can simply destroy existing widgets:

    def open_settings_window():
        for widget in window.winfo_children():
            widget.destroy()
    
        window.title("Settings Hub")  # Update title
    
        # Create the new widgets for the settings page
        label = customtkinter.CTkLabel(window, text="test",)
        label.pack(pady=10)