Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a collapsible pane in Tkinter requires a bit of manual setup since there isn't a built-in widget for this purpose. However, with some creativity, we can achieve this using a combination of frames, buttons, and some logic.
Here's a tutorial to create a collapsible pane in Tkinter:
Setting up the basic GUI with Tkinter:
Start with a basic window using Tkinter
.
import tkinter as tk root = tk.Tk() root.title("Collapsible Pane in Tkinter") root.geometry("400x400")
Create a CollapsiblePane class:
We'll design a CollapsiblePane
class that will be our custom widget.
class CollapsiblePane(tk.Frame): def __init__(self, master, **kwargs): tk.Frame.__init__(self, master, **kwargs) self.toggle_button = tk.Button(self, text="Open Pane", command=self.toggle) self.toggle_button.pack(fill=tk.X) self.pane = tk.Frame(self) self.opened = False def toggle(self): if self.opened: self.pane.pack_forget() self.toggle_button.config(text="Open Pane") else: self.pane.pack(fill=tk.BOTH, expand=True) self.toggle_button.config(text="Close Pane") self.opened = not self.opened
The CollapsiblePane
class contains a button (toggle_button
) and a frame (pane
). The button toggles the visibility of the frame.
Using the CollapsiblePane:
Create an instance of the CollapsiblePane
and populate its inner frame with some content to demonstrate its functionality.
collapsible = CollapsiblePane(root) collapsible.pack(pady=20, padx=20, fill=tk.BOTH, expand=True) for i in range(10): tk.Label(collapsible.pane, text=f"Content {i+1}").pack(pady=2)
Run the main loop:
root.mainloop()
By integrating the above code snippets and executing the script, you'll encounter a Tkinter window with a button labeled "Open Pane". Clicking on this button will expand the pane to reveal the content inside it. Clicking again will collapse the pane, hiding its content. This approach offers a simple way to implement a collapsible pane in Tkinter. If desired, you can further enhance the UI/UX by adding animations, styling, or more interactivity.
How to create collapsible panes in Tkinter:
import tkinter as tk def toggle_pane(): if frame.winfo_ismapped(): frame.grid_remove() else: frame.grid() root = tk.Tk() # Create a collapsible pane frame = tk.Frame(root, width=200, height=100, bg='lightblue') frame.grid(row=0, column=0, padx=10, pady=10) # Toggle button toggle_button = tk.Button(root, text="Toggle Pane", command=toggle_pane) toggle_button.grid(row=1, column=0, pady=10) root.mainloop()
Tkinter collapsible frame example:
import tkinter as tk def toggle_frame(): if frame.winfo_ismapped(): frame.grid_remove() else: frame.grid() root = tk.Tk() # Create a collapsible frame frame = tk.Frame(root, width=200, height=100, bg='lightblue') frame.grid(row=0, column=0, padx=10, pady=10) # Toggle button toggle_button = tk.Button(root, text="Toggle Frame", command=toggle_frame) toggle_button.grid(row=1, column=0, pady=10) root.mainloop()
Expandable and collapsible panes in Tkinter:
import tkinter as tk def toggle_pane(): current_state = frame.grid_info() if current_state: frame.grid_forget() else: frame.grid(row=0, column=0, padx=10, pady=10) root = tk.Tk() # Create an expandable and collapsible pane frame = tk.Frame(root, width=200, height=100, bg='lightblue') frame.grid(row=0, column=0, padx=10, pady=10) # Toggle button toggle_button = tk.Button(root, text="Toggle Pane", command=toggle_pane) toggle_button.grid(row=1, column=0, pady=10) root.mainloop()
Creating accordion-style panes in Tkinter:
import tkinter as tk def toggle_pane(pane): current_state = panes[pane].winfo_ismapped() if current_state: panes[pane].grid_remove() else: panes[pane].grid(row=0, column=pane, padx=10, pady=10) root = tk.Tk() # Create accordion-style panes panes = {} for i in range(3): panes[i] = tk.Frame(root, width=200, height=100, bg=f'lightblue') panes[i].grid(row=0, column=i, padx=10, pady=10) # Toggle button for each pane toggle_button = tk.Button(root, text=f"Toggle Pane {i}", command=lambda i=i: toggle_pane(i)) toggle_button.grid(row=1, column=i, pady=10) root.mainloop()
Customizing collapsible panes in Tkinter:
import tkinter as tk def toggle_pane(): if frame.winfo_ismapped(): frame.grid_remove() else: frame.grid() root = tk.Tk() # Customize collapsible pane frame = tk.Frame(root, width=200, height=100, bg='lightblue', relief=tk.SOLID, bd=2) frame.grid(row=0, column=0, padx=10, pady=10) # Toggle button toggle_button = tk.Button(root, text="Toggle Pane", command=toggle_pane) toggle_button.grid(row=1, column=0, pady=10) root.mainloop()
Toggleable frames in Tkinter with collapse feature:
import tkinter as tk class ToggleableFrame(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) self.visible = True def toggle(self): if self.visible: self.grid_remove() else: self.grid() self.visible = not self.visible root = tk.Tk() # Create toggleable frames frame1 = ToggleableFrame(root, width=200, height=100, bg='lightblue') frame1.grid(row=0, column=0, padx=10, pady=10) frame2 = ToggleableFrame(root, width=200, height=100, bg='lightgreen') frame2.grid(row=0, column=1, padx=10, pady=10) # Toggle buttons toggle_button1 = tk.Button(root, text="Toggle Frame 1", command=frame1.toggle) toggle_button1.grid(row=1, column=0, pady=10) toggle_button2 = tk.Button(root, text="Toggle Frame 2", command=frame2.toggle) toggle_button2.grid(row=1, column=1, pady=10) root.mainloop()
Adding collapsible sections to Tkinter GUI:
import tkinter as tk def toggle_section(section): current_state = sections[section].winfo_ismapped() if current_state: sections[section].grid_remove() else: sections[section].grid(row=0, column=section, padx=10, pady=10) root = tk.Tk() # Add collapsible sections to the Tkinter GUI sections = {} for i in range(3): sections[i] = tk.Frame(root, width=200, height=100, bg=f'lightblue') sections[i].grid(row=0, column=i, padx=10, pady=10) # Toggle button for each section toggle_button = tk.Button(root, text=f"Toggle Section {i}", command=lambda i=i: toggle_section(i)) toggle_button.grid(row=1, column=i, pady=10) root.mainloop()
Tkinter collapsible pane with toggle button:
import tkinter as tk class CollapsiblePane(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) self.visible = True def toggle(self): if self.visible: self.grid_remove() else: self.grid() self.visible = not self.visible root = tk.Tk() # Create a Tkinter collapsible pane with toggle button collapsible_pane = CollapsiblePane(root, width=200, height=100, bg='lightblue') collapsible_pane.grid(row=0, column=0, padx=10, pady=10) # Toggle button toggle_button = tk.Button(root, text="Toggle Pane", command=collapsible_pane.toggle) toggle_button.grid(row=1, column=0, pady=10) root.mainloop()
Dynamic resizable panes in Tkinter:
import tkinter as tk def resize_pane(event): new_width = event.width new_height = event.height frame.config(width=new_width, height=new_height) root = tk.Tk() # Create a resizable pane frame = tk.Frame(root, width=200, height=100, bg='lightblue') frame.grid(row=0, column=0, padx=10, pady=10) frame.bind("<Configure>", resize_pane) root.mainloop()