
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to add padding to a tkinter widget only on one side?
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.
- Related Articles
- How to force Tkinter text widget to stay on one line?
- How to add a column to a Tkinter TreeView widget?
- How to make the Tkinter text widget read only?
- Add advanced features to a tkinter Text widget
- How to center a Tkinter widget?
- How to set focus on Entry widget in Tkinter?
- How to make a Tkinter widget invisible?
- How to select only one Radiobutton in Tkinter?
- How to clear Text widget content while clicking on the Entry widget itself in Tkinter?
- How to remove focus from a Tkinter widget?
- How to update a Python/tkinter label widget?
- How to update a Button widget in Tkinter?
- How to connect a variable to the Tkinter Entry widget?
- How to set focus for Tkinter widget?
- How to erase everything from a Tkinter text widget?

Advertisements