Tkinter grid row and column
grid is a way to wrap and position window components in a grid or Excel spreadsheet-like manner.
The syntax format of the grid method is as follows:
grid(options, ...)
The options parameter can be row, column, padx/pady, rowspan, columnspan, sticky.
The concept of row and column parameters can be found in the following figure:
The position of the window component can be wrapped by modestly adjusting the row and column values within the grid( ) method.
Tkinter grid row and column Example#1
Use the grid() method instead of the pack() method:
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="lightyellow Label",
bg="lightyellow",width=15)
lab2 = Label(window,text="lightgreen Label",
bg="lightgreen",width=15)
lab3 = Label(window,text="lightblue Label",
bg="lightblue",width=15)
lab1.grid(row=0,column=0)
lab2.grid(row=1,column=0)
lab3.grid(row=1,column=1)
window.mainloop()
Output:
Tkinter grid row and column Example#2
Experience another application of grid.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="lightyellow Label",
bg="lightyellow",width=15)
lab2 = Label(window,text="lightgreen Label",
bg="lightgreen",width=15)
lab3 = Label(window,text="lightblue Label",
bg="lightblue",width=15)
lab1.grid(row=0,column=0)
lab2.grid(row=1,column=2)
lab3.grid(row=2,column=1)
window.mainloop()
Output: