I used a for loop to place buttons for a calculator in Tkinter. The idea is simple: every button is placed in a specific row and column, based on its value. This is the code (I wanted to make it generic):
for i in range(10):
btn = tk.Button(root,
text=str(i),
command=lambda: expression.set(expression.get() + str(i)),
width=10,
height=2
)
row = 3 - (i - 1) // 3
col = i % 3
col += 3 if not col else 0
col = col if i else 2
btn.grid(row=row, columns=col)
#print(f"{i}: {row}, {col}") ## Debug
nums.append(btn)
Here's a screenshow of the result:
I tried to print rows and columns and the output is:
0: 4, 2
1: 3, 1
2: 3, 2
3: 3, 3
4: 2, 1
5: 2, 2
6: 2, 3
7: 1, 1
8: 1, 2
9: 1, 3
Which seems okay, so I guess there's something I don't understand in Tkinter grid.

command=lambda: ...tocommand=lambda i=i: .... It will save you a few min of debugging.