Tkinter: How to set size for empty row or column in grid().
As default empty row/column has height/width 0 (zero) and you don't see this row/column but it exists.
import tkinter as tk root = tk.Tk() l1 = tk.Label(root, text='Col:8 Row:1', bg='red') l1.grid(column=8, row=1) l2 = tk.Label(root, text='Col:1 Row:8', bg='red') l2.grid(column=1, row=8) root.mainloop()
You could put Label with spaces and new lines (\n) as text
import tkinter as tk root = tk.Tk() l1 = tk.Label(root, text='Col:8 Row:1', bg='red') l1.grid(column=8, row=1) l2 = tk.Label(root, text='Col:1 Row:8', bg='red') l2.grid(column=1, row=8) # add empty label in row 0 and column 0 l0 = tk.Label(root, text=' \n ', bg='green') l0.grid(column=0, row=0) # add empty label in row 9 and column 9 l9 = tk.Label(root, text=' ', bg='green') l9.grid(column=9, row=9) root.mainloop()
Or Label without text but with width/height
import tkinter as tk root = tk.Tk() l1 = tk.Label(root, text='Col:8 Row:1', bg='red') l1.grid(column=8, row=1) l2 = tk.Label(root, text='Col:1 Row:8', bg='red') l2.grid(column=1, row=8) # add empty label in row 0 and column 0 l0 = tk.Label(root, width=3, height=3, bg='green') l0.grid(column=0, row=0) l0 = tk.Label(root, width=3, height=3, bg='green') l0.grid(column=9, row=9) root.mainloop()
but both use width/height in chars, not pixels.
You can set minimal size for row or column (ie. row/column number 9) using
root.rowconfigure(9, minsize=30)
root.columnconfigure(9, minsize=30)
or
root.rowconfigure(9, {'minsize': 30})
root.columnconfigure(9, {'minsize': 30})
import tkinter as tk root = tk.Tk() l1 = tk.Label(root, text='Col:8 Row:1', bg='red') l1.grid(column=8, row=1) l2 = tk.Label(root, text='Col:1 Row:8', bg='red') l2.grid(column=1, row=8) # set minimal size for row 9 and column 9 root.rowconfigure(0, minsize=30) root.rowconfigure(9, minsize=30) root.columnconfigure(0, minsize=30) root.columnconfigure(9, minsize=30) root.mainloop()
Notes:
If you use grid() in other widget ie. frame.grid(...) then you have to use frame.rowconfigure(...) and frame.columnconfigure
Stackoverflow: How to set a certain amount of rows and columns of a Tkinter grid?
If you like it
Buy a Coffee
Buy a Coffee