Tkinter Label Padding
When designing labels or other widget controls, if you don’t set the size of the widget, the system will use the optimal space as the size of this widget, which was introduced in section 2-3 as a way to establish the widget size. In fact, you can also change the label area by setting the spacing between the label text and the label area. padx can set the x-axis spacing between the left and right boundaries of the label text and the label area, and pady can set the y-axis spacing between the upper and lower boundaries of the label text and the label area.
Tkinter Label Padding Example
To give the reader a clearer understanding of the meaning of padx/pady, this program sets the background of the label to light yellow, then sets the left and right spacing between the label text and the label area to 15, and the top and bottom spacing between the label text and the label area to 20.
"""
Set the border of the control
"""
from tkinter import *
root = Tk()
root.title("apidemos.com")
# relief_str = "flat"
# relief_str = "groove"
relief_str = "raised"
# relief_str = "ridge"
# relief_str = "solid"
# relief_str = "sunken"
label = Label(root,text="sunken",relief=relief_str,
bg="lightyellow",padx=15,pady=20) # Set the border of the widget control
label.pack() # Packaging and positioning components
root.mainloop()
Output: