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.
#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()
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.