How to set a certain number of rows and columns of a Tkinter grid?

Published December 30, 2021

In Tkinter, you can change the look of the graphical user interface by choosing a different geometry manager. In Tkinter, the grid geometry manager is among the most prevalent ways to make things look like they should. It is used to show where widgets should be in an application with a 2D geometry form.

A grid geometry manager lets you choose how many columns and rows there are and where to put the application widget. To set a certain number of columns and rows, you must give the magnitude of the row and column setup that helps you figure out where a widget should go.

In this post, we'll look at how to specify the number of columns and rows in a Tkinter grid.

Take a look at the code snippet below. We're going to be building a label widget and use the grid geometry manager to set the position and the size of the window

from tkinter import*

win = Tk()

win.geometry("500x250")

label1 = Label(win, text='Position1', font=("Calibri, 12"))

label1.grid(column=1, row=2)

label2 = Label(win, text='Position2', font=("Calibri, 12"))

label2.grid(column=3, row=5)

label3 = Label(win, text='Position3', font=("Calibri, 12"))

label3.grid(column=5, row=8)

label4 = Label(win, text='Position4', font=("Calibri, 12"))

label4.grid(column=7, row=11)

win.rowconfigure(9)

win.columnconfigure(9)

win.mainloop()

 

 

Output:

How to set a certain number of rows and columns of a Tkinter grid

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

638 Views