Python 3 – Tkinter PanedWindow
Introduction
Tkinter is a standard GUI (Graphical User Interface) library for Python. It provides tools for creating windows, buttons, menus, etc. Tkinter widget PanedWindow
is a window containing two or more panes that can be resized by dragging the handle (a separator line).
Creating a PanedWindow
Creating a PanedWindow
is easy. The first step is to create an instance of the PanedWindow
class, and then add child widgets to it. Here is the sample code:
from tkinter import *
root = Tk()
# Create a PanedWindow
my_paned_window = PanedWindow(root, orient=HORIZONTAL)
my_paned_window.pack(fill=BOTH, expand=1)
# Add child widgets to the PanedWindow
left_frame = Frame(my_paned_window, bd=1, relief=SUNKEN)
my_paned_window.add(left_frame)
right_frame = Frame(my_paned_window, bd=1, relief=SUNKEN)
my_paned_window.add(right_frame)
# Start the main event loop
root.mainloop()
The code creates a horizontal PanedWindow
and adds two child Frames
to it. The bd
option specifies the border width, while the relief
option specifies the border type.
Setting Resizing Policy
By default, PanedWindow
resizes all child panes evenly. However, you can override this behavior by setting the sashwidth
and sashrelief
options, both of which adjust the appearance of the sash. Here is the sample code:
# Create a PanedWindow with a different sash appearance
my_paned_window = PanedWindow(root, orient=HORIZONTAL, sashwidth=5, sashrelief=SUNKEN)
This code creates a horizontal PanedWindow
with a sash width of 5 pixels and a sunken sash relief.
You can also set the resize
option of a pane to override the resize behavior of the PanedWindow
. The options are:
NONE
: disable resizeFIRST
: resize only the first paneLAST
: resize only the last pane
# Create a PanedWindow with different resizing policy
my_paned_window = PanedWindow(root, orient=VERTICAL)
my_paned_window.pack(fill=BOTH, expand=1)
top_frame = Frame(my_paned_window, bd=1, relief=SUNKEN)
my_paned_window.add(top_frame, resize=False)
bottom_frame = Frame(my_paned_window, bd=1, relief=SUNKEN)
my_paned_window.add(bottom_frame, resize=True) # this is the default
This code creates a vertical PanedWindow
with two child Frames
. The resize
option of the top Frame
is set to False
, so it won’t be affected by the resize action.
Changing Pane Sizes
You can use the panecget()
and paneconfig()
methods to get or set the options of a pane. The method panes()
returns a list of all panes. Here is the sample code:
# Change pane sizes
my_paned_window = PanedWindow(root, orient=HORIZONTAL)
my_paned_window.pack(fill=BOTH, expand=1)
left_frame = Frame(my_paned_window, bd=1, relief=SUNKEN)
my_paned_window.add(left_frame, minsize=100)
right_frame = Frame(my_paned_window, bd=1, relief=SUNKEN)
my_paned_window.add(right_frame, minsize=50)
# Change the size of the first pane
my_paned_window.paneconfig(left_frame, minsize=150)
# Get pane options
print(my_paned_window.panecget(left_frame, "minsize"))
This code creates a horizontal PanedWindow
with two child Frames
. The minsize
option specifies the minimum size of a pane. The first Frame
is added with a minimum size of 100, and the second Frame
with a minimum size of 50.
Then the size of the first pane is changed from 100 to 150. Finally, the current value of the minsize
option of the first pane is printed (150
).
Conclusion
In this article, we went through how Tkinter PanedWindow
works in Python 3. We learned how to create and set the resizing policy of PanedWindow
, and how to change the size of panes dynamically.
Tkinter’s PanedWindow
widget is a flexible and useful tool for creating resizable GUIs with multiple panes.