Tkinter PanedWindow weight parameter
Design three LabelFrame objects as children of PanedWindow and arrange them horizontally.
In the tkinter.ttk module, if you execute add (child object, options), you can add the weight parameter in the options field, weight represents the ratio of each Pane change when changing the width of the window, if you insert three child objects LabelFrame when the weight is 1, which means that when zooming in or out of the window, the three child object is the same proportion.
Note that you need to import thikter.ttk when using the weight parameter in the add( ) method.
Example#1
When inserting three LabelFrame objects add weight=1. If you zoom in or out of the window during execution, you can see that the three LabelFrame sub-objects are changed in the same proportion. Note that line 3 of the program is importing tkinter.ttk, which is required otherwise the program will have compilation errors.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("apidemos.com")
# pw = PanedWindow(root,orient=HORIZONTAL) # Create PanedWindow Object
pw = PanedWindow(orient=HORIZONTAL) # Create PanedWindow Object
leftframe = LabelFrame(pw,text="Left Pane",width=120,height=150)
pw.add(leftframe,weight=1) # Insert Left LabelFrame
middleframe = LabelFrame(pw,text="Middle Pane",width=120)
pw.add(middleframe,weight=1) # Insert Middle LabelFrame
rightframe = LabelFrame(pw,text="Right Pane",width=120)
pw.add(rightframe,weight=1) # Insert Right LabelFrame
pw.pack(fill=BOTH,expand=True,padx=10,pady=10)
root.mainloop()
Output:
If the three LabelFrame sub-objects set different weights, they will have different effects on each other when the window size is changed later.
Example#2
This means that when changing the width, the change ratio is 2:2:1 respectively.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("apidemos.com")
# pw = PanedWindow(root,orient=HORIZONTAL) # Create PanedWindow Object
pw = PanedWindow(orient=HORIZONTAL) # Create PanedWindow Object
leftframe = LabelFrame(pw,text="Left Pane",width=120,height=150)
pw.add(leftframe,weight=2) # Insert Left LabelFrame
middleframe = LabelFrame(pw,text="Middle Pane",width=120)
pw.add(middleframe,weight=2) # Insert Middle LabelFrame
rightframe = LabelFrame(pw,text="Right Pane",width=120)
pw.add(rightframe,weight=1) # Insert Right LabelFrame
pw.pack(fill=BOTH,expand=True,padx=10,pady=10)
root.mainloop()
Output: