Last Updated : 15 Mar, 2021
In this article, we will discuss the procedure of adding padding to a Tkinter widget only on one side. Here, we create a widget and use widget.grid() method in tkinter to padding the content of the widget. For example, let's create a label and use label.grid() method. Below the syntax is given:
label1 = Widget_Name(app, text="text_to_be_written_in_label") label1.grid( padx=(padding_from_left_side, padding_from_right_side), pady=(padding_from_top, padding_from_bottom))
Steps Needed:
from tkinter import *
app= Tk()
app.title(“Name of GUI app”)
l1 =Widget_Name(app, text="Text we want to give in widget")
l1.grid(padx=(padding from left side, padding from right side), pady=(padding from top, padding from bottom))
l1.grid(padx=(0, 0), pady=(200, 0))
app.mainloop( )
Example 1: Padding at left-side to a widget
Python
# Python program to add padding
# to a widget only on left-side
# Import the library tkinter
from tkinter import *
# Create a GUI app
app = Tk()
# Give title to your GUI app
app.title("Vinayak App")
# Maximize the window screen
width = app.winfo_screenwidth()
height = app.winfo_screenheight()
app.geometry("%dx%d" % (width, height))
# Construct the label in your app
l1 = Label(app, text='Geeks For Geeks')
# Give the leftmost padding
l1.grid(padx=(200, 0), pady=(0, 0))
# Make the loop for displaying app
app.mainloop()
Output:
Example 2: Padding from top to a widget
Python
# Python program to add padding
# to a widget only from top
# Import the library tkinter
from tkinter import *
# Create a GUI app
app = Tk()
# Give title to your GUI app
app.title("Vinayak App")
# Maximize the window screen
width = app.winfo_screenwidth()
height = app.winfo_screenheight()
app.geometry("%dx%d" % (width, height))
# Construct the button in your app
b1 = Button(app, text='Click Here!')
# Give the topmost padding
b1.grid(padx=(0, 0), pady=(200, 0))
# Make the loop for displaying app
app.mainloop()
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4