有没有一种方法可以使通过 for 循环生成的按钮跨越两列

Is there a way of making a button that was generated through a for loop span two colums

我一直在通过 for 循环为计算器创建按钮。但是,我想让两个按钮跨越两列。

我知道通过创建单独的按钮我们可以编写

zero = Button(btns_frame, text = "0", fg = "black", width = 21, height = 3, bd = 0, bg = "#fff", cursor = "hand2",activebackground = "#1E90FF", command = lambda: btn_eval(0))
zero.grid(row = 5, column = 0, columnspan = 2, padx = 1, pady = 1)
 buttons = ['M+', 'M-', 'MR', 'MC',
            'CA', 'Del', '/', 'hi',
            '7', '8', '9', '*',
            '4', '5', '6', '-',
            '1', '2', '3', '+',
            '0', '.', '=', 'bye'
               ]
          count = 0

          for row in range(2, 10):
               for column in range(4):
                    button = Button(window, width = 10, fg = "black", height = 3, bd = 0, bg = '#fff',
                                    cursor = 'hand2', activebackground = '#1e90ff', text = buttons[count],
                                    command = lambda i=buttons[count]: self.functions(i)).grid(row=row, column=column)
                    count += 1

我只是放置 "hi" 和 "bye" 只是为了保留 space 但我想要跨越 2 列的按钮是 "CA" 和零

使用 ternary condition (or a simple if) 相应地设置列跨度应该是可能的:

buttons = ['M+', 'M-', 'MR', 'MC',
            'CA', 'xxxxx', 'Del', '/',  # the xxxxx will be skipped
            '7', '8', '9', '*',
            '4', '5', '6', '-',
            '1', '2', '3', '+',
            '0', 'xxxxx', '.', '='   
               ]
count = 0


for row in range(2, 10):
    for column in range(4):
        if (row,column) in [(3,1),(9,1)]: # the xxxxx positions, no need to make a button 
            continue 
        cs = 2 if buttons[count] in ("0","CA") else 1
        button = Button(window, width = 10, fg = "black", height = 3, bd = 0, bg = '#fff',
                        cursor = 'hand2', activebackground = '#1e90ff', 
                        text = buttons[count],
                        command = lambda i=buttons[count]: self.functions(i)).grid(row=row, 
                                                            column=column, columnspan = cs)
        count += cs # to make indexes still work as is, increase by 2 when needed