In this Tkinter Tutorial, we will discuss the PanedWindow Class. The PanedWindow Class (or Widget) is used very similar to Frames, as a container widget which can store other widgets (and even nested PanedWindows).
How to use Tkinter PanedWindow?
To create a PanedWindow in Tkinter, we must use the PanedWindow Class. There are many different parameters that this Class can take. Let us discuss a few of the important ones that you will want to use often.
parent:The compulsory first parameter required in every tkinter widget. Specifies the container in which the widget will be stored.sashrelief: This can be used to change the look of the “sash” which divides a window into multiple panes. If you are on Windows, then make sure to keep this value totk.RAISED, else the draggable sash will not be visible.showhandle: Displays a small “handle” in the form of a square on the sash.- width: Size in pixels (horizontally)
- height: Size in pixels (vertically)
This is what a standard definition for a PanedWindow might look like.
mainpanel = tk.PanedWindow(master,
sashrelief=tk.RAISED,
showhandle=True,
width=300,
height=300)There is one more important thing to remember about PanedWindows. Unlike Frames, where we just have to specify the Frame as the “parent” (to place widgets inside it), with PanedWindows we need to perform one additional operation with the add() method.
For example, we have a PanedWindow object called mainpanel and we have a label object called label_1. After creating this label, we need to add it to the mainpanel in the following manner.
mainpanel.add(label_1)With this knowledge in mind, let us take a look at a proper example with some output.
import tkinter as tk
class Window():
def __init__(self, master):
mainpanel = tk.PanedWindow(master,
sashrelief=tk.RAISED,
showhandle=True,
width=300,
height=300)
mainpanel.pack(fill=tk.BOTH, expand=True)
label_1 = tk.Label(mainpanel, text="Main Panel")
label_2 = tk.Label(mainpanel, text="Other Text")
mainpanel.add(label_1)
mainpanel.add(label_2)
root = tk.Tk()
window = Window(root)
root.mainloop()
