tkinter
  1. tkinter-panedwindow

PanedWindow

The PanedWindow widget in Tkinter is used to place one or more child widgets in a horizontal or vertical pane, where each pane can be resized by the user.

Syntax

The basic syntax for creating a PanedWindow is as follows:

paned_window = tk.PanedWindow(parent, **options)

Here, parent is the widget or container in which the PanedWindow will be placed, and options are the configuration options for the PanedWindow.

Example

Consider the following example, which creates a horizontal PanedWindow with two panes, each containing a Label widget:

import tkinter as tk

root = tk.Tk()
root.geometry('400x300')

paned_window = tk.PanedWindow(root, orient=tk.HORIZONTAL)
paned_window.pack(fill=tk.BOTH, expand=1)

left_pane = tk.Label(paned_window, text='Left Pane', bg='yellow')
paned_window.add(left_pane, weight=1)

right_pane = tk.Label(paned_window, text='Right Pane', bg='blue')
paned_window.add(right_pane, weight=2)

root.mainloop()

In this example, we create a PanedWindow with orient=tk.HORIZONTAL to make a horizontal pane. We create two panes and add them to the PanedWindow using the add() method, with the weight option to control each pane's resizing behavior.

Output

The output of the above example is a horizontal PanedWindow widget with two panes, where the right pane takes up twice as much space as the left pane. The user can drag the divider between the panes to resize them.

Explanation

The PanedWindow widget in Tkinter is a container that allows for one or more child widgets to be placed in horizontal or vertical panes. The add() method is used to add child widgets to the PanedWindow, and the orient option is used to set the direction of the panes (either tk.HORIZONTAL or tk.VERTICAL). The weight option is used to control how much space each pane should take up when the PanedWindow is resized by the user.

Use

The PanedWindow widget is useful in situations where you want to allow the user to resize a portion of your GUI dynamically. For example, you may want to create a split-screen interface where the user can adjust the of each pane to their liking.

Important Points

  • PanedWindow is a container widget that can be used to create resizable panes in a GUI.
  • The add() method is used to add child widgets to the PanedWindow.
  • The orient option is used to set the direction of the panes (either tk.HORIZONTAL or tk.VERTICAL).
  • The weight option is used to control how much space each pane should take up when the PanedWindow is resized.

Summary

The PanedWindow widget in Tkinter is a useful container widget that allows for dynamically resizable panes in a GUI. It can be used to create split-screen interfaces or other complex layouts with resizable elements. With the add() method and the weight option, it is easy to add child widgets to the PanedWindow and control the resizing behavior of each pane.

Published on: