Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The PanedWindow
widget in tkinter
is a container widget that can contain one or more child widgets. Its primary feature is that it provides a handle (or "sash") that users can drag to adjust the relative sizes of the child widgets. This makes it useful for creating resizable panes in a GUI application.
PanedWindow
Widget in tkinter
Tutorial:1. Import Required Libraries:
import tkinter as tk from tkinter import ttk
2. Create the Main Application Window:
root = tk.Tk() root.title("PanedWindow Example")
3. Creating a PanedWindow
:
The PanedWindow
can be oriented either horizontally (tk.HORIZONTAL
) or vertically (tk.VERTICAL
).
# Creating a vertical PanedWindow pane = tk.PanedWindow(root, orient=tk.VERTICAL) pane.pack(fill=tk.BOTH, expand=1)
4. Adding Widgets to the PanedWindow
:
# First child: A Text widget text1 = tk.Text(pane, height=5, bg="lightgray") pane.add(text1) # Second child: Another Text widget text2 = tk.Text(pane, height=5, bg="lightblue") pane.add(text2)
You can add any widget to a PanedWindow
, and it doesn't have to be just two. You can add as many as you'd like.
5. (Optional) Configuring the PanedWindow
:
sashrelief
: Determines the relief style of the sash. Can be tk.RAISED
, tk.SUNKEN
, and so on.sashwidth
: Determines the width of the sash.pane.config(sashrelief=tk.RAISED, sashwidth=10)
6. Run the Main Loop:
root.mainloop()
Complete Code:
import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("PanedWindow Example") # Creating a vertical PanedWindow pane = tk.PanedWindow(root, orient=tk.VERTICAL) pane.pack(fill=tk.BOTH, expand=1) # First child: A Text widget text1 = tk.Text(pane, height=5, bg="lightgray") pane.add(text1) # Second child: Another Text widget text2 = tk.Text(pane, height=5, bg="lightblue") pane.add(text2) # Configuring the PanedWindow pane.config(sashrelief=tk.RAISED, sashwidth=10) root.mainloop()
When you run the above code, you'll see a window with two Text
widgets separated by a sash. You can click and drag the sash to adjust the relative sizes of the two Text
widgets.
The PanedWindow
widget in Tkinter is used to create a container with panes that can be resized using a separator called a sash.
Example Code:
import tkinter as tk root = tk.Tk() paned_window = tk.PanedWindow(root, orient="horizontal") frame1 = tk.Frame(paned_window, background="lightblue", width=100) frame2 = tk.Frame(paned_window, background="lightgreen", width=100) paned_window.add(frame1) paned_window.add(frame2) paned_window.pack(fill="both", expand=True) root.mainloop()
The PanedWindow
widget is used to create a container that can hold multiple panes. The panes can be arranged horizontally or vertically, and a separator (sash) allows users to resize the panes.
The PanedWindow
widget has options like orient
, sashwidth
, sashrelief
, and more for configuring its behavior. These options determine the orientation of the panes and appearance of the sash.
Example Code:
import tkinter as tk root = tk.Tk() paned_window = tk.PanedWindow(root, orient="vertical", sashwidth=8, sashrelief="raised") frame1 = tk.Frame(paned_window, background="lightblue", height=100) frame2 = tk.Frame(paned_window, background="lightgreen", height=100) paned_window.add(frame1) paned_window.add(frame2) paned_window.pack(fill="both", expand=True) root.mainloop()
The PanedWindow
widget enables the creation of resizable panes. Users can drag the sash to adjust the size of the panes dynamically.
Example Code:
import tkinter as tk root = tk.Tk() paned_window = tk.PanedWindow(root, orient="horizontal") frame1 = tk.Frame(paned_window, background="lightblue", width=100) frame2 = tk.Frame(paned_window, background="lightgreen", width=100) paned_window.add(frame1) paned_window.add(frame2) paned_window.pack(fill="both", expand=True) root.mainloop()
The position of the sash in a PanedWindow
can be set or retrieved using the sashposition()
method. This allows you to programmatically control the initial position of the sash.
Example Code:
import tkinter as tk def move_sash(): paned_window.sash_position(0, 150) # Set the position of the sash for the first pane root = tk.Tk() paned_window = tk.PanedWindow(root, orient="horizontal") frame1 = tk.Frame(paned_window, background="lightblue", width=100) frame2 = tk.Frame(paned_window, background="lightgreen", width=100) paned_window.add(frame1) paned_window.add(frame2) paned_window.pack(fill="both", expand=True) move_button = tk.Button(root, text="Move Sash", command=move_sash) move_button.pack() root.mainloop()
The appearance of the PanedWindow
can be customized by setting options like sashwidth
, sashrelief
, and configuring the panes with background colors, sizes, etc.
Example Code:
import tkinter as tk root = tk.Tk() paned_window = tk.PanedWindow(root, orient="horizontal", sashwidth=8, sashrelief="raised") frame1 = tk.Frame(paned_window, background="lightblue", width=100) frame2 = tk.Frame(paned_window, background="lightgreen", width=100) paned_window.add(frame1) paned_window.add(frame2) paned_window.pack(fill="both", expand=True) root.mainloop()
The PanedWindow
widget provides a simple way to manage resizable panes in Tkinter. Panes can be added or removed dynamically to create a flexible layout.
Example Code:
import tkinter as tk root = tk.Tk() paned_window = tk.PanedWindow(root, orient="horizontal") frame1 = tk.Frame(paned_window, background="lightblue", width=100) frame2 = tk.Frame(paned_window, background="lightgreen", width=100) paned_window.add(frame1) paned_window.add(frame2) paned_window.pack(fill="both", expand=True) def add_pane(): new_frame = tk.Frame(paned_window, background="lightcoral", width=100) paned_window.add(new_frame) add_button = tk.Button(root, text="Add Pane", command=add_pane) add_button.pack() root.mainloop()
The PanedWindow
can be configured to have either horizontal or vertical orientation. This allows you to create layouts with resizable panes in both directions.
Example Code:
import tkinter as tk root = tk.Tk() # Horizontal PanedWindow paned_window_horizontal = tk.PanedWindow(root, orient="horizontal") frame1 = tk.Frame(paned_window_horizontal, background="lightblue", width=100) frame2 = tk.Frame(paned_window_horizontal, background="lightgreen", width=100) paned_window_horizontal.add(frame1) paned_window_horizontal.add(frame2) paned_window_horizontal.pack(fill="both", expand=True) # Vertical PanedWindow paned_window_vertical = tk.PanedWindow(root, orient="vertical") frame3 = tk.Frame(paned_window_vertical, background="lightcoral", height=100) frame4 = tk.Frame(paned_window_vertical, background="lightyellow", height=100) paned_window_vertical.add(frame3) paned_window_vertical.add(frame4) paned_window_vertical.pack(fill="both", expand=True) root.mainloop()