
- 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 horizontally center a widget using a grid() in Tkinter?
As the name suggests, a grid is nothing but a set of rows and columns. Tkinter grid manager works similarly; it places the widget in a 2-dimensional plane to align the device through its position vertically and horizontally.
Let us consider an example where we want to make the widget centered in the window while resizing it. The grid(row, column) property will help to make the label widget centered horizontally, and the sticky property will avoid resizing the widget in the window.
Example
#Import tkinter library from tkinter import * #Create an instance of Tkinter frame or window win= Tk() #Set the geometry of tkinter frame win.geometry("750x250") #Make the window sticky for every case win.grid_rowconfigure(0, weight=1) win.grid_columnconfigure(0, weight=1) #Create a Label label=Label(win, text="This is a Centered Text",font=('Aerial 15 bold')) label.grid(row=2, column=0) label.grid_rowconfigure(1, weight=1) label.grid_columnconfigure(1, weight=1) win.mainloop()
Output
The above code will display a window containing a text label widget centered horizontally. When we resize the window, it will not affect the widget’s position.
- Related Articles
- How to center a Tkinter widget?
- How to center a Tkinter widget in a sticky frame?
- How do I center the text in a Tkinter Text widget?
- How to horizontally center a in another ?
- How to center a Text object horizontally on canvas using FabricJS?
- How to call a function using the OptionMenu widget in Tkinter?
- How to make a Button using the Tkinter Canvas widget?
- How to center a Text object horizontally and vertically on canvas using FabricJS?
- How to update a Button widget in Tkinter?
- FabricJS – How to center a Line object horizontally on a canvas?
- How to make a Tkinter widget invisible?
- How to center an annotation horizontally over a point in Matplotlib?
- How to temporarily remove a Tkinter widget without using just .place?
- How to see if a widget exists in Tkinter?
- How to create hyperlink in a Tkinter Text widget?

Advertisements