Python collapsible sidebar on tkinter [closed]

Here is a simple example of how to create a collapsible frame. You can use the same method to create a collapsible sidebar by putting all your sidebar stuff in the frame.

import tkinter as tk


class Example(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('300x100')
        self.left_frame = tk.Frame(self)
        self.left_frame.grid(row=0, column=0, sticky='nsew')
        self.right_frame = tk.Frame(self)
        self.rowconfigure(0, weight=1)
        for i in range(3):
            self.columnconfigure(i, weight=1)

        tk.Label(self.left_frame, text="Left Frame").grid(row=0, column=0, sticky='nsew')
        tk.Label(self.right_frame, text="Right Frame").grid(row=0, column=0, sticky='nsew')

        self.frame_status = False
        self.ar_btn = tk.Button(self, text="▶", width=1, command=self.toggle_right_frame)
        self.ar_btn.grid(row=0, column=2, sticky='nse')

    def toggle_right_frame(self):
        if self.frame_status:
            self.right_frame.grid_forget()
            self.frame_status = False
            self.ar_btn.config(text="▶")
        else:
            self.frame_status = True
            self.right_frame.grid(row=0, column=1, sticky='nsew')
            self.ar_btn.config(text="◀")

Example().mainloop()

Results:

enter image description here

enter image description here

Leave a Comment