Open In App

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

Last Updated : 15 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  • First, import the library tkinter
from tkinter import *
  • Now, create a GUI app using tkinter
app= Tk()
  • Next, give a title to the app.
app.title(“Name of GUI app”)
  • Then, create the widget by replacing the #Widget Name with the name of the widget (such as Label, Button, etc.).
l1 =Widget_Name(app, text="Text we want to give in widget")
  • Moreover, give the padding where we want to give it.
l1.grid(padx=(padding from left side, padding from right side),
    pady=(padding from top, padding from bottom))
  • For instance, if we want to give the padding from the top side only, then enter the padding value at the specified position, and leave the remaining as zero. It will give the padding to a widget from one side only, i.e., top.
l1.grid(padx=(0, 0), pady=(200, 0))
  • Finally, make the loop for displaying the GUI app on the screen.
app.mainloop( )
  • It will give the output as follows:

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:

padding tkinter

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:

padding tkinter python



Previous Article
Next Article

Similar Reads

Tkinter - Read only Entry Widget
Python has a number of frameworks to develop GUI applications like PyQT, Kivy, Jython, WxPython, PyGUI, and Tkinter. Python tkinter module offers a variety of options to develop GUI based applications. Tkinter is an open-source and available under Python license. Tkinter provides the simplest and fastest way to develop a GUI application. Tkinter su
4 min read
How To Make The Tkinter Text Widget Read Only?
The Tkinter library in Python provides a powerful and flexible way to create GUI applications. One of the widgets available in Tkinter is the Text widget, which is used for displaying and editing multi-line text. However, there might be situations where you want the Text widget to be read-only, preventing users from modifying its content. This arti
3 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Place plots side by side in Matplotlib
Matplotlib is the most popular Python library for plotting graphs and visualizing our data. In Matplotlib we can create multiple plots by calling them once. To create multiple plots we use the subplot function of pyplot module in Matplotlib. Syntax: plt.subplot(nrows, .ncolumns, index) Parameters: nrows is for number of rows means if the row is 1 t
3 min read
Tkinter | Adding style to the input text using ttk.Entry widget
Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself. Entry widgets are used to get the entry from the user. It can be created as follows- entry = ttk.Entry(master, option = value, ...) Code #1: Creating Entry widget and taking input from user (taking only String d
2 min read
Python | Menu widget in Tkinter
Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used package for GUI applications which comes with the Python itself. Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipu
2 min read
Progressbar widget in Tkinter | Python
The purpose of this widget is to reassure the user that something is happening. It can operate in one of two modes - In determinate mode, the widget shows an indicator that moves from beginning to end under program control. In indeterminate mode, the widget is animated so the user will believe that something is in progress. In this mode, the indica
2 min read
Python | PanedWindow Widget in Tkinter
Tkinter supports a variety of widgets to make GUI more and more attractive and functional. The PanedWindow widget is a geometry manager widget, which can contain one or more child widgets panes. The child widgets can be resized by the user, by moving separator lines sashes using the mouse. Syntax: PanedWindow(master, **options) Parameters: master:
3 min read
How to change background color of Tkinter OptionMenu widget?
Prerequisites: Tkinter While creating GUI applications, there occur various instances in which you need to make selections among various options available. For making such choices, the Option Menu widget was introduced. In this article, we will be discussing the procedure of changing menu background color of Tkinter’s option Menu widget. To achieve
2 min read
Python Tkinter - Toplevel Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in Python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Toplevel widget A Toplevel widget is used to create a window on top of all other w
3 min read
Practice Tags :
three90RightbarBannerImg