Tkinter pack expand parameter
The expand parameter sets whether the widget control fills the additional parent container space:
- The default is False (or 0), which means it is not filled.
- If it is True (or 1), it is filled.
Tkinter pack expand Example#1
Use the expand=True parameter in the lightblue Label
and observe the execution results.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="lightyellow Lable",
bg="lightyellow")
lab2 = Label(window,text="lightgreen Label",
bg="lightgreen")
lab3 = Label(window,text="lightblue Label",
bg="lightblue")
lab1.pack(side=LEFT,fill=Y)
lab2.pack(fill=X)
lab3.pack(fill=BOTH,expand=True)
window.mainloop()
Output:
Having read this far, the reader should understand that the side, fill and expand parameters affect each other.
Tkinter pack expand Example#2
Configure the tabs from top to bottom to appreciate the application of the expand parameter and the fill parameter.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x200")
Label(root,text="Mississippi",bg="red",fg="white",
font="Times 24 bold").pack(fill=X)
Label(root,text="Kentucky",bg="green",fg="white",
font="Arial 24 bold italic").pack(fill=BOTH,expand=True)
Label(root,text="Purdue",bg="blue",fg="white",
font="Times 24 bold").pack(fill=X)
root.mainloop()
Output:
Tkinter pack expand Example#3
Configure the tabs from left to right to experience the application of the expand parameter and the fill parameter.
from tkinter import *
root = Tk()
root.title("apidemos.com")
# root.geometry("300x200")
Label(root,text="Mississippi",bg="red",fg="white",
font="Times 20 bold").pack(side=LEFT,fill=Y)
Label(root,text="Kentucky",bg="green",fg="white",
font="Arial 20 bold italic").pack(side=LEFT,fill=BOTH,expand=True)
Label(root,text="Purdue",bg="blue",fg="white",
font="Times 20 bold").pack(side=LEFT,fill=Y)
root.mainloop()
Output: