我正在尝试将AC按钮和“0”按钮放大。如何在不搞乱我的for循环的情况下做到这一点?我尝试过了柱子,它对我不起作用。
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)
答案 0 :(得分:0)
也许有一个按钮名称和大小值的字典:
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()参数。
答案 1 :(得分:0)
你需要给AC和零按钮一个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()
这样做的好处是更加明确,而且不那么苛刻。