Tkinter Window Padding

Tkinter Window – Set Padding

In Tkinter, you can set required padding for a window along X-axis and Y-axis using the parameters/options of the Tk().

Tkinter Window - Set Padding
Lightgreen area is the padding of the window

The padding area of the window remains vacant and is used provide a space between the edges of the window and the body of the window.

To set padding for a window in Tkinter, set the padx and pady options of the Tk window object. padx option sets the padding along X-axis, and pady option sets the padding along Y-axis.

window['padx'] = 100
window['pady'] = 50

In this tutorial, you will learn how to set the padding for a Tkinter window, with an example program.

Example

Consider a simple use case where you would like to create a Tk window, with a padding of 100 along X-axis (horizontal), and a padding of 50 along Y-axis (vertical).

We have configured the background of the window, and also created a label widget such that the label widget occupies more width and height than the available space in window, thus revealing the padding of the window.

Python Program

import tkinter as tk
from tkinter import font

# Create the main window
window = tk.Tk()
window.geometry("500x500")
window.title("PythonExamples.org")
window['bg'] = 'lightgreen'

# Set window padding

# Along X-axis
window['padx'] = 100

# Along Y-axis
window['pady'] = 50

label_font = font.Font(size=80)
label = tk.Label(window, wraplength=400, font=label_font, text="This is a sample text to visulaize the padding of a window.")
label.pack()

# Run the application
window.mainloop()

Output in Windows

Output in MacOS

Tkinter Window - Set Padding
Lightgreen area is the padding of the window

Summary

In this Python Tkinter tutorial, we learned how to set a title for window in Tkinter, with examples.

Related Tutorials

Code copied to clipboard successfully 👍