สมมติว่าเราต้องการเพิ่ม Padding บนด้านเดียว (Top/Bottom หรือ Left/Right) ของ Widget เฉพาะตัว เราสามารถทำได้ใน Tkinter โดยใช้เมธอด pack() และ grid()
ในเมธอด pack() เราต้องกำหนดค่า "padx" และ "pady" ส่วนเมธอด Grid ต้องการแค่ Tuple 2 ค่า คือ X และ Y สำหรับเพิ่ม Padding รอบแกน X หรือแกน Y
ตัวอย่าง
# import the required library
from tkinter import *
# Create an instance of window or frame
win = Tk()
win.geometry("700x400")
# Create two buttons
# Add padding in x and y axis
b1 = Button(win, text="Button1", font=('Poppins bold', 15))
b1.pack(padx=10)
b2 = Button(win, text="Button2", font=('Poppins bold', 15))
b2.pack(pady=50)
b3 = Button(win, text="Button3", font=('Poppins bold', 15))
b3.pack(padx=50, pady=50)
# Keep running the window
win.mainloop()
ผลลัพธ์
การรันโค้ดข้างต้นจะสร้างหน้าต่างที่มี 3 ปุ่ม ซึ่งมี Padding รอบแกน X, Y หรือทั้งสองแกน
