I am trying to understand the use of the TKinter grid, and more especially the rowspan and columnspan arguments.
I have created a small test to understand Tkinter’s grid system. I want to have two different frames, each with its own grid layout. In the second subframe, I have labels with rowspans of 1 and 2. I expect the labels with rowspan=2 to be twice the height of those with rowspan=1, but they are not:
from tkinter import *
import tkinter as tk
root = Tk()
mainframe = tk.Frame(root)
mainframe.grid(row=0, column=0)
for i in range(2):
mainframe.rowconfigure(i, weight=1)
mainframe.columnconfigure(i, weight=1)
subframe1 = tk.Frame(mainframe)
subframe1.grid(column=0, row=0, sticky="nswe")
for i in range(4):
subframe1.rowconfigure(i, weight=1)
for i in range(2):
subframe1.columnconfigure(i, weight=1)
subframe2 = tk.Frame(mainframe)
subframe2.grid(column=1, row=0, sticky="nswe")
for i in range(4):
subframe2.rowconfigure(i, weight=1)
for i in range(2):
subframe2.columnconfigure(i, weight=1)
tk.Label(subframe1, text="Label 1", bg="gray").grid(column=0, row=0, sticky="nswe")
tk.Label(subframe1, text="Label 2", bg="red").grid(column=1, row=0, sticky="nswe")
tk.Label(subframe1, text="Label 3", bg="cyan").grid(column=0, row=1, sticky="nswe")
tk.Label(subframe1, text="Label 4", bg="blue").grid(column=1, row=1, sticky="nswe")
tk.Label(subframe1, text="Label 9", bg="gray").grid(column=0, row=2, sticky="nswe")
tk.Label(subframe1, text="Label 10", bg="red").grid(column=1, row=2, sticky="nswe")
tk.Label(subframe1, text="Label 11", bg="cyan").grid(column=0, row=3, sticky="nswe")
tk.Label(subframe1, text="Label 12", bg="blue").grid(column=1, row=3, sticky="nswe")
tk.Label(subframe2, text="Label 5", bg="yellow").grid(column=0, row=0, rowspan=2, sticky="nswe")
tk.Label(subframe2, text="Label 6", bg="grey").grid(column=1, row=0, rowspan=1, sticky="nswe")
tk.Label(subframe2, text="Label 7", bg="red").grid(column=0, row=2, rowspan=1, sticky="nswe")
tk.Label(subframe2, text="Label 8", bg="purple").grid(column=1, row=2, rowspan=2, sticky="nswe")
mainframe.mainloop()
Here’s the result
Can someone please explain to me why labels 6 and 7 are not half the size of labels 5 and 8?
Antoine Brehon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.