Tkinter pack
An application need more widget controls, so that's when it comes to how to configure these widget controls into containers or windows.
When designing a GUI program, there are three ways to wrap and position components within a container or window, also known as window control configuration managers, often called layout methods.
There are three layout methods: pack
, grid
, and place
, the most commonly used is the pack()
method, which is described in this article.
The syntax format of the tkinter pack() method is as follows
.pack(options, ...)
Options parameters can be the following properties:
- side
- fill
- padx/pady
- padx/ipady
- chor
Related course: Python Desktop Apps with Tkinter
tkinter pack() side
Controls can be configured vertically or horizontally, and you can set the order in which they are arranged
#coding:UTF-8
#ch1.py
from tkinter import *
root = Tk()
root.title ("Normal Settings Control")
Label(root, text="Tkinter", bg="lightyellow").pack()
Label(root, text="wxPython", bg="lightgreen").pack()
Label(root, text="PyQt", bg="lightblue").pack()
root.mainloop()
If there are multiple controls in the window, the pack method automatically lets the controls appear from top to bottom, which is the default setting. You can also use the side parameter to change the arrangement, which is valued as follows
- TOP: The default, arranged from top to bottom
- BOTTOM: Arrange from bottom to top
- LEFT: Arrange from left to right
- RIGHT: From right to left
With a different width:
#coding:UTF-8
#ch2.py --ch1.py 2.0
from tkinter import *
root = Tk()
root.title ("Normal Settings Control")
Label(root, text="Tkinter", bg="lightyellow", width=15).pack(side=BOTTOM)
Label(root, text="wxPython", bg="lightgreen", width=15).pack(side=BOTTOM)
Label(root, text="PyQt", bg="lightblue", width=15).pack(side=BOTTOM)
root.mainloop()
Change it to LEFT
#coding:UTF-8。
#ch3.py -- ch2.py->side->LEFT。
from tkinter import *
root = Tk()
root.title ("Normal Settings Control")
Label(root, text="Tkinter", bg="lightyellow", width=15).pack(side=LEFT)
Label(root, text="wxPython", bg="lightgreen", width=15).pack(side=LEFT)
Label(root, text="PyQt", bg="lightblue", width=15).pack(side=LEFT)
root.mainloop()
Hybrid layout
#coding:UTF-8
#ch4.py -- ch3.py->-side-> hybrid
from tkinter import *
root = Tk()
root.title ("Normal Settings Control")
Label(root, text="Tkinter", bg="lightyellow", width=15).pack()
Label(root, text="wxPython", bg="lightgreen", width=15).pack(side=RIGHT)
Label(root, text="PyQt", bg="lightblue", width=15).pack(side=LEFT)
root.mainloop()
The padx /pady parameter
You can use the padx/pady parameter to set the distance between the control boundary and the container (which can be thought of as a window boundary) or between the control boundaries.
The distance between window controls is 1 pixel in the default environment, and if you want a moderate spacing, you can set padx/pady to represent horizontal spacing/vertical spacing
For Instance:
#coding:UTF-8。
from tkinter import *
root = Tk()
root.title("relief")
Label(root, text="flat", relief="flat").pack(side=LEFT, pady=5, padx=4, ipadx=10, ipady=10)
Label(root, text="groove", relief="groove").pack(side=LEFT, pady=5, padx=4, ipadx=10, ipady=10)
Label(root, text="raised", relief="raised").pack(side=LEFT, pady=5, padx=4, ipadx=10, ipady=10)
Label(root, text="ridge", relief="ridge").pack(side=LEFT, pady=5, padx=4, ipadx=10, ipady=10)
Label(root, text="solid", relief="solid").pack(side=LEFT, pady=5, padx=4, ipadx=10, ipady=10)
Label(root, text="sunken", relief="sunken").pack(
side=LEFT, pady=5, padx=4, ipadx=10, ipady=10)
root.mainloop()
ipadx / ipady parameters
You can set the position of the Widget control in the window, with the following parameters
#coding: UTF-8。
#ch7.py anchor。
from tkinter import *
root = Tk()
root.title("ch7.py")
root.geometry("300x180")
okbtn = Button(root, text="OK",
font="Times 20 bold",
fg="white", bg="blue")
okbtn.pack(anchor=S, side=RIGHT,
padx=10, pady=10)
root.mainloop()
Related course: Python Desktop Apps with Tkinter
anchor
You can set the position of the Widget control in the window, with the following parameters
#coding: UTF-8
#ch7.py anchor
from tkinter import *
root = Tk()
root.title("ch7.py")
root.geometry("300x180")
okbtn = Button(root, text="OK",
font="Times 20 bold",
fg="white", bg="blue")
okbtn.pack(anchor=S, side=RIGHT,
padx=10, pady=10)
root.mainloop()
Another example
#coding: UTF-8
#ch8.py anchor + NO BTN
from tkinter import *
root = Tk()
root.title("ch7.py")
root.geometry("300x180")
okbtn = Button(root, text="OK",
font="Times 20 bold",
fg="white", bg="blue")
okbtn.pack(anchor=S, side=RIGHT,
padx=10, pady=10)
nobtn = Button(root, text = "NO",
font="Times 20 bold",
fg="white", bg="red")
nobtn.pack(anchor=S, side=RIGHT,
pady=10)
root.mainloop()
fill
This parameter tells the pack manager how to set the control to fill the allocated container interval, if fills the allocated space X-axis is not white, and vice versa, if the fill-in-the-box X-axis indicates that the control can fill the allocated space X-axis and Y-axis. The default value for fill is None, which means that the original size is maintained.
#coding:UTF-8
#ch9.py fill
from tkinter import *
root = Tk()
root.title("tkinter fill")
Label(root, text="Tkinter", bg="lightyellow").pack(fill=X)
Label(root, text="wxPython", bg="lightgreen").pack()
Label(root, text="PyQt", bg="lightblue").pack(fill=X)
root.mainloop()
expand
The expand parameter sets whether the Widget control fills up the additional parent container space, which defaults to False (or 0), indicating that it is not filled, or if it is True (or 1). It's important!
#coding:UTF-8
#ch9.py fill
from tkinter import *
root = Tk()
root.title("expand")
Label(root, text="Tkinter", bg="lightyellow").pack(side=LEFT, fill=Y)
Label(root, text="wxPython", bg="lightgreen").pack(fill=X)
Label(root, text="PyQt", bg="lightblue").pack(fill=BOTH, expand=True)
root.mainloop()
Related course: Python Desktop Apps with Tkinter