0

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:

Output

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.

1
  • Not related to your immediate problem but change command=lambda: ... to command=lambda i=i: .... It will save you a few min of debugging. Commented Jan 9, 2023 at 22:16

1 Answer 1

1

You have a typo in this line:

btn.grid(row=row, columns=col)

columns (plural) is treated as a shortcut for columnspan. Since you don't specify the column with column (singular), all buttons are going in column zero with a columnspan equal to whatever is in col.

TL;DR: change columns=col to column=col.

Sign up to request clarification or add additional context in comments.

6 Comments

Do you know why that shortcut exists? columns doesn't appear to be in the tcl docs. In the docs it's specified as columnspan. I know about the bd/border shortcut but sometimes bd stands for something else - I think. I don't see the point to those shortcuts.
@TheLizzard: the shortcut exists because the underlying tcl/tk interpreter supports abbreviating all options to the shortest unique string possible. You can find a mention of it in the options man page: "Command-line switches may be abbreviated, as long as the abbreviation is unambiguous. " In that context, "command-line switches" refers to options of a tk command (eg: -background, -command, etc). I don't see it mentioned anywhere in the official tkinter documentation.
Didn't know that was part of tcl but column and columns are too close imo. I am surprised that I haven't made this typo before. colspan and rowspan seem more logical abbreviations. Also tkinter's official docs aren't well designed and are missing quite a bit of info.
@TheLizzard: I agree, the tkinter docs are a bit shallow. I suspect that when they first wrote them, they assumed the reader was already very familiar with tcl/tk. Also, this isn't a tcl feature, it's specific to how tk handles command options.
Welp, I will have to read more about tk vs tcl and how they handle arguments. Thx for the info.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.