The PanedWindow widget Methods in tkinter
In this lesson, we covered the basics of PanedWindow in Tkinter, including its syntax, options, and methods. We explored how to create a PanedWindow widget and add panes to it, as well as how to adjust the size and position of panes and the divider between them.
We also discussed some common uses for PanedWindow in Tkinter, such as creating resizable interfaces and building multi-pane applications. Finally, we included some multiple-choice questions and answers to help reinforce the concepts covered in this lesson.
There are several methods associated with the PanedWindow widget in Tkinter.
Adds a pane to the PanedWindow.
paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2)
In this example, two panes are added to the PanedWindow using the add method.
Removes a pane from the PanedWindow.
paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.forget(pane1)
In this example, two panes are added to the PanedWindow using the add method, and then the first pane is removed using the forget method.
Note that the forget method removes the pane from the PanedWindow, but does not destroy the widget itself. If you want to completely remove the widget from the application, you should call the destroy method on the widget.
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.forget(pane1) pane1.destroy() root.mainloop()
This creates a horizontal PanedWindow with two panes, removes the first pane using the forget method, and then destroys the widget using the destroy method.
another example
paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.forget(pane2)
In this example, two panes are added to the PanedWindow, and then the second pane is removed using the forget method.
forget method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Remove pane 2 from the PanedWindow paned_window.forget(pane2) root.mainloop()
In this example, two panes are added to the PanedWindow, and then the forget method is called to remove the second pane from the PanedWindow.
Returns the current position of the sash (the divider between the panes) in pixels.
paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) sash_pos = paned_window.sash_coord(0)
In this example, two panes are added to the PanedWindow, and then the position of the sash is retrieved using the sash_coord method.
Sets the position of the sash to the specified pixel position.
paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.sash_place(0, 200)
In this example, two panes are added to the PanedWindow, and then the position of the sash is set to 200 pixels using the sash_place method.
sash_position method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root, orient=VERTICAL) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Set the sash position of the PanedWindow paned_window.sash_position(50) root.mainloop()
In this example, a PanedWindow is created with the VERTICAL orientation, and two panes are added to it. The sash_position method is then called to set the position of the sash at the middle of the PanedWindow.
Get or set options for a pane within the PanedWindow.
paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) pane1.config(bg='green')
In this example, two panes are added to the PanedWindow, and then the background color of the first pane is changed to green using the config method, which is equivalent to calling the paneconfigure method with the first pane as an argument.
Note that the panecget and paneconfigure methods can be used to get or set any option for a pane within the PanedWindow.
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Get the current position of the sash sash_pos = paned_window.sash_coord(0) print('Current sash position:', sash_pos) # Set the position of the sash paned_window.sash_place(0, 200) # Get the current background color of pane 1 bg_color = pane1.cget('bg') print('Current background color of pane 1:', bg_color) # Change the background color of pane 1 pane1.config(bg='green') root.mainloop()
Returns the index of the pane containing the specified widget.
paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) index = paned_window.identify(pane2)
In this example, two panes are added to the PanedWindow, and then the index of the second pane is retrieved using the identify method.
identify method another example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Get the index of pane 2 in the PanedWindow index = paned_window.identify(pane2) print('Index of pane 2:', index) root.mainloop()
In this example, two panes are added to the PanedWindow, and then the identify method is called to retrieve the index of the second pane in the PanedWindow.
Returns a proxy object for a pane, which can be used to configure the pane without actually
adding it to the PanedWindow.
paned_window = PanedWindow(root) proxy = paned_window.proxy() pane1 = Label(proxy, text='Pane 1', bg='red') pane2 = Label(proxy, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2)
proxy method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) proxy = paned_window.proxy() pane1 = Label(proxy, text='Pane 1', bg='red') pane2 = Label(proxy, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Configure the proxy object proxy.config(sashwidth=10) root.mainloop()
panes method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Get the list of panes in the PanedWindow panes = paned_window.panes() for pane in panes: print(pane) root.mainloop()
configure method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Configure the PanedWindow paned_window.configure(bg='white') root.mainloop()
In this example, a PanedWindow is created with two panes, and the configure method is called to set the background color of the PanedWindow to white.
forget method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Remove the second pane from the PanedWindow paned_window.forget(pane2) root.mainloop()
In this example, a PanedWindow is created with two panes, and the forget method is called to remove the second pane from the PanedWindow.
panecget method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Get the background color of the first pane bg_color = paned_window.panecget(pane1, 'bg') print(bg_color) root.mainloop()
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Get the background color of the first pane bg_color = paned_window.panecget(pane1, 'bg') print(bg_color) root.mainloop()
panedheight and panedwidth methods example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Get the height and width of the sash sash_height = paned_window.panedheight() sash_width = paned_window.panedwidth() print(sash_height, sash_width) root.mainloop()
sash_coord method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root, orient=VERTICAL) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Get the coordinates of the sash sash_coords = paned_window.sash_coord(0) print(sash_coords) root.mainloop()
sash_place method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Move the sash to a new position paned_window.sash_place(0, 200) root.mainloop()
sash_relplace method example:
from tkinter import * root = Tk() paned_window = PanedWindow(root) pane1 = Label(paned_window, text='Pane 1', bg='red') pane2 = Label(paned_window, text='Pane 2', bg='blue') paned_window.add(pane1) paned_window.add(pane2) paned_window.pack(fill=BOTH, expand=True) # Move the sash to a new relative position paned_window.sash_relplace(0, 0.3) root.mainloop()
add(child, options)
The add(child, options) method is used to add a new child widget to a PanedWindow. The method takes two arguments:
child: the widget to add as a child.
options: a dictionary of options for the new child widget.
The following options can be used:
before:
specifies the index of the child widget to insert the new child widget before. If not specified, the new child widget will be added at the end.
after:
specifies the index of the child widget to insert the new child widget after. If not specified, the new child widget will be added at the end.
stretch:
specifies whether the new child widget should be resized when the PanedWindow is resized. Can be set to YES or NO. Default is YES.
Here’s an example that adds a Button widget to a PanedWindow using the add() method:
from tkinter import * root = Tk() paned_window = PanedWindow(root) paned_window.pack(fill=BOTH, expand=True) button1 = Button(paned_window, text="Button 1") paned_window.add(button1) button2 = Button(paned_window, text="Button 2") paned_window.add(button2, stretch=YES) root.mainloop()
get(startindex, endindex)
The get(startindex, endindex) method is used to retrieve information about the size and position of one or panes in a PanedWindow. The method takes two arguments:
startindex:
the index of the first pane to retrieve information for.
endindex:
the index of the last pane to retrieve information for.
The method returns a tuple containing the requested information.
The exact contents of the tuple depend on the options used when the panes were added to the PanedWindow.
Here’s an example that uses the get() method to retrieve the size of the first pane in a PanedWindow:
from tkinter import * root = Tk() paned_window = PanedWindow(root) paned_window.pack(fill=BOTH, expand=True) button1 = Button(paned_window, text="Button 1") paned_window.add(button1) button2 = Button(paned_window, text="Button 2") paned_window.add(button2, stretch=YES) size = paned_window.get(0) print(size) root.mainloop()
config(options)
The config(options) method is used to configure the options of a PanedWindow. The method takes a dictionary of options and their values.
Here’s an example that uses the config() method to change the orientation of a PanedWindow to vertical:
from tkinter import * root = Tk() paned_window = PanedWindow(root, orient=HORIZONTAL) paned_window.pack(fill=BOTH, expand=True) button1 = Button(paned_window, text="Button 1") paned_window.add(button1) button2 = Button(paned_window, text="Button 2") paned_window.add(button2, stretch=YES) paned_window.config(orient=VERTICAL) root.mainloop()
The uses of Tkinter PanedWindow with code examples
The PanedWindow widget in Tkinter is useful for creating resizable panes that can be used to display multiple widgets in a single window. Here are some examples of how to use PanedWindow:
Creating a split-pane window:
from tkinter import * root = Tk() paned_window = PanedWindow(root, orient=HORIZONTAL) paned_window.pack(fill=BOTH, expand=True) left_label = Label(paned_window, text="Left pane") paned_window.add(left_label) right_label = Label(paned_window, text="Right pane") paned_window.add(right_label) root.mainloop()
Creating a nested split-pane window:
from tkinter import * root = Tk() outer_paned_window = PanedWindow(root, orient=HORIZONTAL) outer_paned_window.pack(fill=BOTH, expand=True) left_label = Label(outer_paned_window, text="Left pane") outer_paned_window.add(left_label) inner_paned_window = PanedWindow(outer_paned_window, orient=VERTICAL) outer_paned_window.add(inner_paned_window) top_label = Label(inner_paned_window, text="Top pane") inner_paned_window.add(top_label) bottom_label = Label(inner_paned_window, text="Bottom pane") inner_paned_window.add(bottom_label) root.mainloop()
Creating a resizable grid of widgets:
from tkinter import * root = Tk() paned_window = PanedWindow(root, orient=VERTICAL) paned_window.pack(fill=BOTH, expand=True) top_pane = PanedWindow(paned_window, orient=HORIZONTAL) paned_window.add(top_pane, stretch=TRUE) bottom_pane = PanedWindow(paned_window, orient=HORIZONTAL) paned_window.add(bottom_pane, stretch=TRUE) button1 = Button(top_pane, text="Button 1") top_pane.add(button1, stretch=TRUE) button2 = Button(top_pane, text="Button 2") top_pane.add(button2, stretch=TRUE) button3 = Button(bottom_pane, text="Button 3") bottom_pane.add(button3, stretch=TRUE) button4 = Button(bottom_pane, text="Button 4") bottom_pane.add(button4, stretch=TRUE) root.mainloop()
Creating a resizable image viewer:
from tkinter import * from PIL import Image, ImageTk root = Tk() image = Image.open("example.jpg") photo = ImageTk.PhotoImage(image) paned_window = PanedWindow(root, orient=VERTICAL) paned_window.pack(fill=BOTH, expand=True) canvas = Canvas(paned_window) canvas.create_image(0, 0, anchor=NW, image=photo) paned_window.add(canvas, stretch=TRUE) scrollbar = Scrollbar(paned_window, command=canvas.yview) paned_window.add(scrollbar, stretch=FALSE) canvas.configure(yscrollcommand=scrollbar.set) root.mainloop()
Creating a resizable text editor:
from tkinter import * root = Tk() paned_window = PanedWindow(root, orient=VERTICAL) paned_window.pack(fill=BOTH, expand=True) text = Text(paned_window) paned_window.add(text, stretch=TRUE) scrollbar = Scrollbar(paned_window, command=text.yview) paned_window.add(scrollbar, stretch=FALSE) text.configure(yscrollcommand=scrollbar.set) root.mainloop()
Creating a resizable calculator:
Here’s an example of how to create a resizable calculator using PanedWindow in Tkinter:
from tkinter import * root = Tk() paned_window = PanedWindow(root, orient=VERTICAL) paned_window.pack(fill=BOTH, expand=True) entry = Entry(paned_window, justify=RIGHT) paned_window.add(entry, stretch=TRUE) button_frame = Frame(paned_window) paned_window.add(button_frame, stretch=TRUE) button_frame.columnconfigure(0, weight=1) button_frame.columnconfigure(1, weight=1) button_frame.columnconfigure(2, weight=1) button_frame.columnconfigure(3, weight=1) button1 = Button(button_frame, text="1") button1.grid(row=0, column=0, sticky="NESW") button2 = Button(button_frame, text="2") button2.grid(row=0, column=1, sticky="NESW") button3 = Button(button_frame, text="3") button3.grid(row=0, column=2, sticky="NESW") button4 = Button(button_frame, text="4") button4.grid(row=1, column=0, sticky="NESW") button5 = Button(button_frame, text="5") button5.grid(row=1, column=1, sticky="NESW") button6 = Button(button_frame, text="6") button6.grid(row=1, column=2, sticky="NESW") button7 = Button(button_frame, text="7") button7.grid(row=2, column=0, sticky="NESW") button8 = Button(button_frame, text="8") button8.grid(row=2, column=1, sticky="NESW") button9 = Button(button_frame, text="9") button9.grid(row=2, column=2, sticky="NESW") button0 = Button(button_frame, text="0") button0.grid(row=3, column=1, sticky="NESW") plus_button = Button(button_frame, text="+") plus_button.grid(row=0, column=3, rowspan=2, sticky="NESW") minus_button = Button(button_frame, text="-") minus_button.grid(row=2, column=3, rowspan=2, sticky="NESW") equal_button = Button(button_frame, text="=") equal_button.grid(row=3, column=2, sticky="NESW") clear_button = Button(button_frame, text="C") clear_button.grid(row=3, column=0, sticky="NESW") root.mainloop()
In this example, a PanedWindow is created with vertical orientation.
complete application by PanedWindow
Here’s an example application that uses PanedWindow in Tkinter.
It’s a simple text editor with a resizable file explorer pane and a text editing pane.
import tkinter as tk import os class TextEditor: def __init__(self, master): self.master = master master.title("Text Editor") master.geometry("800x600") # Create paned window self.panedwindow = tk.PanedWindow(self.master, orient=tk.HORIZONTAL) self.panedwindow.pack(fill=tk.BOTH, expand=True) # Create file explorer pane self.file_explorer = tk.Frame(self.panedwindow, width=200, height=600) self.file_explorer.pack(fill=tk.BOTH, expand=True) self.panedwindow.add(self.file_explorer) # Create scrollbar for file explorer self.scrollbar = tk.Scrollbar(self.file_explorer, orient=tk.VERTICAL) self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # Create file explorer listbox self.listbox = tk.Listbox(self.file_explorer, yscrollcommand=self.scrollbar.set) self.listbox.pack(fill=tk.BOTH, expand=True) self.listbox.bind("<Double-Button-1>", self.open_file) # Set scrollbar to listbox self.scrollbar.config(command=self.listbox.yview) # Create text editing pane self.text_pane = tk.Text(self.panedwindow) self.panedwindow.add(self.text_pane) # Bind events for file explorer pane self.file_explorer.bind("<Configure>", self.resize_file_explorer) self.listbox.bind("<ButtonRelease-1>", self.show_selection) # Populate file explorer self.populate_file_explorer() def populate_file_explorer(self): # Get files in current directory files = os.listdir() # Add files to listbox for file in files: if os.path.isfile(file): self.listbox.insert(tk.END, file) def open_file(self, event): # Get filename from listbox selection selection = self.listbox.curselection() filename = self.listbox.get(selection) # Open file and populate text pane with open(filename, "r") as file: contents = file.read() self.text_pane.delete("1.0", tk.END) self.text_pane.insert(tk.END, contents) def resize_file_explorer(self, event): # Resize scrollbar and listbox to fill file explorer pane self.scrollbar.configure(width=20) self.scrollbar.place(x=self.file_explorer.winfo_width() - 20, y=0, height=self.file_explorer.winfo_height()) self.listbox.configure(width=self.file_explorer.winfo_width()-20) def show_selection(self, event): # Get selected item in listbox and print to console selection = self.listbox.curselection() if selection: index = selection[0] data = self.listbox.get(index) print(data) root = tk.Tk() text_editor = TextEditor(root) root.mainloop()
A) A widget used for displaying tabular data
B) A container widget used for organizing other widgets with a resizable divider
C) A widget used for creating pop-up menus
Answer: B
A) add_child()
B) add()
C) add_widget()
Answer: B
A) minsize
B) mintext
C) minwidth
Answer: A
A) get_divider_position()
B) get_position()
C) get()
Answer: A
A) <Configure>
B) <Resize>
C) <WindowResize>
Answer: A
A) position
B) start
C) initial
Answer: B
A) remove_child()
B) remove()
C) delete()
Answer: B
A) maxsize
B) maxwidth
C) maxtext
Answer: A
A) config()
B) options()
C) configure()
Answer: A
A) orientation
B) orient
C) layout
Answer: B
A) get_count()
B) count()
C) size()
Answer: B
A) border
B) borderwidth
C) bd
Answer: C
A) get_minsize()
B) minsize()
C) get_min()
Answer: B
A) background
B) foreground
C) dividercolor
Answer: A
A) set_divider_position()
B) set_position()
C) set()
Answer: A
A) uniform
B) equal
C) size
Answer: A
A) get_size()
B) size()
C) get()
Answer: B
A) relief
B) style
C) borderstyle
Answer: A
A) adjust()
B) resize()
C) configure()
Answer: B
A) cursor
B) dividercursor
C) hovercursor
Answer: A
A) remove_pane()
B) remove()
C) delete()
Answer: B
A) orientation
B) orient
C) direction
Answer: B
A) set_minsize()
B) minsize()
C) set_min()
Answer: B
A) dividerwidth
B) width
C) size
Answer: A
A) swap()
B) switch()
C) exchange()
Answer: C
A) font
B) panefont
C) pane_font
Answer: A
A) get_divider_position()
B) get_position()
C) position()
Answer: A
A) dividerheight
B) height
C) size
Answer: A
A) adjust_minsize()
B) resize_minsize()
C) configure_minsize()
Answer: B
A) anchor
B) paneanchor
C) pane_anchor
Answer: A
“The Tkinter PanedWindow Widget” TkDocs.com.
“Tkinter PanedWindow Widget” GeeksforGeeks.org
“Tkinter PanedWindow” Programiz.com. [Online]. Available:
“Tkinter PanedWindow widget” effbot.org. [Online]. Available: