

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 center an annotation horizontally over a point in Matplotlib?
- How to make a Tkinter widget invisible?
- How to center a button element vertically and horizontally with CSS?
- How to make a Button using the Tkinter Canvas widget?
- How to update a Button widget in Tkinter?
- How to call a function using the OptionMenu widget in Tkinter?
- How to center your website horizontally with CSS?
- How to temporarily remove a Tkinter widget without using just .place?
- How to update a Python/tkinter label widget?
- How to remove focus from a Tkinter widget?
- How to add a column to a Tkinter TreeView widget?
- How to horizontally center a <div> in another <div>?
Advertisements