1

I want to make "=" button higher so it cover the space above(also wanna make "." wider so it also covers space) what I am getting

so I want to join the cell above "=" and cell next to ".", wanna make one "big" button

    window = tk.Tk()
window.title("Calculator")
window.geometry("400x500")
window.resizable(False, False)

entry = tk.Entry(window, font=("Arial", 30))
entry.grid(row=0, column=0, columnspan=5, sticky="nsew")

buttons = [
    ("1", "2", "3", "C", "/"),
    ("4", "5", "6", "√", "*"),
    ("7", "8", "9", "<-", "+"),
    ("0", "(", ")", "-"),
    (" ", " ", ".", "^", "=")
]

for row_idx, row in enumerate(buttons):
    for col_idx, button_text in enumerate(row):
        if button_text != " ":
            button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
                               command=lambda text=button_text: click(text))
            button.grid(row=row_idx+1, column=col_idx, sticky="nsew")

for i in range(5):
    window.grid_columnconfigure(i, weight=1)

for i in range(len(buttons) + 1):
    window.grid_rowconfigure(i, weight=1)

window.mainloop()
Eric
  • 11
  • 2

2 Answers2

2

Set columnspan and rowspan accordingly.

This has them hard-coded based on the button texts that should span, but you can probably do something more elegant.

buttons = [
    ("1", "2", "3", "C", "/"),
    ("4", "5", "6", "√", "*"),
    ("7", "8", "9", "<-", "+"),
    ("0", "(", ")", "-", "="),
    (".", None, None, "^"),
]

for row_idx, row in enumerate(buttons):
    for col_idx, button_text in enumerate(row):
        if button_text:
            button = tk.Button(
                window,
                text=button_text,
                font=("Arial", 20),
                padx=3,
                pady=2,
                command=lambda text=button_text: click(text),
            )
            button.grid(
                row=row_idx + 1,
                column=col_idx,
                columnspan=3 if button_text == "." else 1,
                rowspan=2 if button_text == "=" else 1,
                sticky="nsew",
            )

enter image description here

AKX
  • 152,115
  • 15
  • 115
  • 172
0

One generic way is using:

  • a tuple to define (text, rowspan, columnspan) options for the button, or
  • a string to define the text option only
  • None for cell that is occupied by other button
buttons = [
    ("1", "2", "3", "C", "/"),
    ("4", "5", "6", "√", "*"),
    ("7", "8", "9", "<-", "+"),
    ("0", "(", ")", "-", ("=",2,1)),
    ((".",1,3), None, None, "^", None)
]

for row_idx, row in enumerate(buttons):
    for col_idx, button_text in enumerate(row):
        if button_text:
            rowspan = colspan = 1
            if type(button_text) is tuple:
                button_text, rowspan, colspan = button_text
            button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
                               command=lambda text=button_text: click(text))
            button.grid(row=row_idx+1, column=col_idx, sticky="nsew", rowspan=rowspan, columnspan=colspan)
acw1668
  • 40,144
  • 5
  • 22
  • 34