Tkinter pack method
Although we call it the pack method, it is actually a class within tkinter. This is the most commonly used control configuration management method. It handles widget control configuration using the concept of relative position, and the correct position of the control is done automatically by the pack method.
The syntax format of the pack method is as follows.
pack(options, ...)
The options parameters can be side, fill, padx/pady, ipadx/ipady, and anchor, and are described in the following subsections.
Tkinter pack side parameter
The side parameter can configure the control vertically or horizontally, see the following program example before going further.
Tkinter pack side parameter Example#1
A window contains three tabs, in the first two chapters of the program to create tk object is to use root as the object name, the object name can be named, this article will intentionally use window as the object name so that readers appreciate.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="lightyellow Label",bg="lightyellow")
lab2 = Label(window,text="lightgreen Label",bg="lightgreen")
lab3 = Label(window,text="lightblue Label",bg="lightblue")
lab1.pack()
lab2.pack()
lab3.pack()
window.mainloop()
Output:
As you can see from the above figure, when there are multiple components in the window, using pack allows the components to be displayed from top to bottom, which is actually the default setting of the system. When using the pack method, you can also add the side parameter to set the arrangement of the components, the value of this parameter is as follows.
-
TOP: This is the default value, which is arranged from top to bottom.
-
BOTTOM: Bottom to top.
-
LEFT: Left to right alignment.
-
RIGHT: Right-to-left alignment.
Tkinter pack side parameter Example#2
Add side=BOTTOM
inside the pack method, and additionally change the width of the label to 15.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="lightyellow Label",bg="lightyellow",width=15) # Set label width
lab2 = Label(window,text="lightgreen Label",bg="lightgreen",width=15)
lab3 = Label(window,text="lightblue Label",bg="lightblue",width=15)
lab1.pack(side=BOTTOM)
lab2.pack(side=BOTTOM)
lab3.pack(side=BOTTOM)
window.mainloop()
Output:
Tkinter pack side parameter Example#3
Add side=LEFT
to the pack method –
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="lightyellow Label",bg="lightyellow",width=15) # 设置标签宽度15
lab2 = Label(window,text="lightgreen Label",bg="lightgreen",width=15) #
lab3 = Label(window,text="lightblue Label",bg="lightblue",width=15) #
lab1.pack(side=LEFT) # Packaging and positioning components
lab2.pack(side=LEFT) #
lab3.pack(side=LEFT) #
window.mainloop()
Output:
Tkinter pack side parameter Example#4
Mixed use of side parameters –
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="lightyellow Label",
bg="lightyellow",width=15) # Set label width 15
lab2 = Label(window,text="lightgreen Label",
bg="lightgreen",width=15) #
lab3 = Label(window,text="lightblue Label",
bg="lightblue",width=15) #
lab1.pack() # Packaging and positioning components
lab2.pack(side=RIGHT) #
lab3.pack(side=LEFT) #
window.mainloop()
Output:
Tkinter pack side parameter Example#5
The common properties of widgets relief are introduced in Tkinter Widget relief, here we will use Here we will use our existing knowledge to list all of its properties.
List all the properties of relief.
from tkinter import *
Reliefs = ["flat","groove","raised","ridge","solid","sunken"]
root = Tk()
root.title("apidemos.com")
for Relief in Reliefs:
Label(
root,text=Relief,relief=Relief,
fg="blue",
font="Times 20 bold"
).pack(side=LEFT,padx=5)
root.mainloop()
Output:
Tkinter pack side parameter Example#6
List all bitmaps bitmaps –
from tkinter import *
bitMaps = ["error","hourglass","info","questhead","question",
"warning","gray12","gray25","gray50","gray75"]
root = Tk()
root.title("apidemos.com")
for bitMap in bitMaps:
Label(
root,bitmap=bitMap
).pack(side=LEFT,padx=5)
root.mainloop()
Output: