How to add padding to a tkinter widget only on one side?

PythonTkinterGUI-Programming

Let us suppose that we want to add padding on one side (either top/bottom or left/right) of a particular widget. We can achieve this in Tkinter by using its pack() and grid() methods.

In pack() method, we have to define the value for “padx” and “pady”. On the other hand, the grid method requires only two tuples, i.e., x and y for adding padding around either of X-axis or Y-axis.

Example

#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()

Output

Running the above code will create a window containing three buttons that will have some padding around either of X, Y, or both axes.

raja
Published on 04-Mar-2021 13:53:33
Advertisements