Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python网格间距计算器应用程序_Python_User Interface_Tkinter_Grid_Calculator - Fatal编程技术网

python网格间距计算器应用程序

python网格间距计算器应用程序,python,user-interface,tkinter,grid,calculator,Python,User Interface,Tkinter,Grid,Calculator,我正在尝试使AC按钮和“0”按钮变大。我怎样才能做到这一点而不打乱我的for循环?我试过columnspan,但它对我不起作用 buttons = [['AC' , '%', '+' ], ['7' , '8' , '9' , '-' ], ['4' , '5' , '6' , '*' ], ['1' , '2' , '3' , '/' ], ['0' , '.' , '=' ]] for r in ran

我正在尝试使AC按钮和“0”按钮变大。我怎样才能做到这一点而不打乱我的for循环?我试过columnspan,但它对我不起作用

buttons = [['AC' , '%', '+' ],
           ['7' , '8' , '9' , '-' ],
           ['4' , '5' , '6' , '*' ],
           ['1' , '2' , '3' , '/' ],
           ['0' , '.' , '=' ]]

for r in range(len(buttons)):
    for c in range(len(buttons[r])):

        def cmd(x = buttons[r][c]):
            self.click(x)

        b = Button(self,
                   text = buttons[r][c],
                   width = 3,
                   relief = RAISED,
                   command = cmd)
        b.grid(row = r + 1, column = c)

可能有按钮名称和大小值的字典:

bsize = {'AC':6,'0':6,'1':3,'2':3 ...}
然后,在定义要放置的尺寸时参考它:

buttons = [['AC' , '%', '+' ],
           ['7' , '8' , '9' , '-' ],
           ['4' , '5' , '6' , '*' ],
           ['1' , '2' , '3' , '/' ],
           ['0' , '.' , '=' ]]

for r in range(len(buttons)):
    for c in range(len(buttons[r])):

        def cmd(x = buttons[r][c]):
            self.click(x)

        b = Button(self,
                   text = buttons[r][c],
                   width = bsize[buttons[r][c]],
                   relief = RAISED,
                   command = cmd)
        b.grid(row = r + 1, column = c)

您可能还需要更改b.grid()参数。

您需要为AC和zero按钮提供一个2的列跨度。这对于您当前的体系结构来说有点尴尬,但您可以尝试以下方法:

buttons = [['AC' , None, '%', '+' ],
           ['7' , '8' , '9' , '-' ],
           ['4' , '5' , '6' , '*' ],
           ['1' , '2' , '3' , '/' ],
           ['0' , None '.' , '=' ]]

for r in range(len(buttons)):
    for c in range(len(buttons[r])):

        if buttons[r][c] is None:
            continue
        def cmd(x = buttons[r][c]):
            self.click(x)

        b = Button(self,
                   text = buttons[r][c],
                   width = 3,
                   relief = RAISED,
                   command = cmd)
        if buttons[r][c] in ['AC', '0']:
            b.grid(row = r + 1, column = c, columnspan=2, sticky='EW')
        else:
            b.grid(row = r + 1, column = c)
buttons = [
    ('AC', 0, 0, 2),
    ('%', 0, 2, 1),
    ('+', 0, 3, 1),
    ('7', 1, 0, 1),
    ('8', 1, 1, 1),
    ('9', 1, 2, 1),
    ('-', 1, 3, 1),
    ('4', 2, 0, 1),
    ('5', 2, 1, 1),
    ('6', 2, 2, 1),
    ('*', 2, 3, 1),
    ('1', 3, 0, 1),
    ('2', 3, 1, 1),
    ('3', 3, 2, 1),
    ('/', 3, 3, 1),
    ('0', 4, 0, 2),
    ('.', 4, 2, 1),
    ('=', 4, 3, 1)]


for label, row, column, span in buttons:
    def cmd(x=label):
        self.click(x)

    b = tkinter.Button(root,
               text = label,
               width = 3,
               relief = tkinter.RAISED,
               command = cmd)
    b.grid(row=row, column=column, columnspan=span, sticky='EW')

root.mainloop()
尽管如此,我还是可以提出类似的建议:

buttons = [['AC' , None, '%', '+' ],
           ['7' , '8' , '9' , '-' ],
           ['4' , '5' , '6' , '*' ],
           ['1' , '2' , '3' , '/' ],
           ['0' , None '.' , '=' ]]

for r in range(len(buttons)):
    for c in range(len(buttons[r])):

        if buttons[r][c] is None:
            continue
        def cmd(x = buttons[r][c]):
            self.click(x)

        b = Button(self,
                   text = buttons[r][c],
                   width = 3,
                   relief = RAISED,
                   command = cmd)
        if buttons[r][c] in ['AC', '0']:
            b.grid(row = r + 1, column = c, columnspan=2, sticky='EW')
        else:
            b.grid(row = r + 1, column = c)
buttons = [
    ('AC', 0, 0, 2),
    ('%', 0, 2, 1),
    ('+', 0, 3, 1),
    ('7', 1, 0, 1),
    ('8', 1, 1, 1),
    ('9', 1, 2, 1),
    ('-', 1, 3, 1),
    ('4', 2, 0, 1),
    ('5', 2, 1, 1),
    ('6', 2, 2, 1),
    ('*', 2, 3, 1),
    ('1', 3, 0, 1),
    ('2', 3, 1, 1),
    ('3', 3, 2, 1),
    ('/', 3, 3, 1),
    ('0', 4, 0, 2),
    ('.', 4, 2, 1),
    ('=', 4, 3, 1)]


for label, row, column, span in buttons:
    def cmd(x=label):
        self.click(x)

    b = tkinter.Button(root,
               text = label,
               width = 3,
               relief = tkinter.RAISED,
               command = cmd)
    b.grid(row=row, column=column, columnspan=span, sticky='EW')

root.mainloop()
这有一个好处,即更明确一点,不那么刻薄