缩短代码或使代码更简洁我的计算器?

时间:2015-09-27 09:38:24

标签: python

有没有办法让生成我的按钮的代码更短?有人可以告诉我该怎么做吗?有任何想法吗?

    Clear = Button(buttons, bg="snow", text="C", width=7, height=1, command=self.clear, relief=RAISED)
    Clear.grid(padx=2, pady=2, column=3, row=0)
    Equals = Button(buttons, bg="snow", text="=", width=7, height=1, command=self.equals, relief=RAISED)
    Equals.grid(padx=2, pady=2, column=4, row=3)
    All_clear = Button(buttons, bg="snow", text="AC", width=7, height=1, command=self.all_clear, relief=RAISED)
    All_clear.grid(padx=2, pady=2, column=4, row=0)
    Bracket_one = Button(buttons, bg="snow", text="(", width=7, height=1, command=self.bracket_one, relief=RAISED)
    Bracket_one.grid(padx=2, pady=2, column=2, row=3)
    Bracket_two = Button(buttons, bg="snow", text=")", width=7, height=1, command=self.bracket_two, relief=RAISED)
    Bracket_two.grid(padx=2, pady=2, column=3, row=3)
    Zero = Button(buttons, bg="snow", text="0", width=7, height=1, command=self.zero, relief=RAISED)
    Zero.grid(padx=2, pady=2, column=0, row=3)
    Decimal_point = Button(buttons, bg="snow", text=".", width=7, height=1, command=self.decimal_point, relief=RAISED)
    Decimal_point.grid(padx=2, pady=2, column=1, row=3)
    Multiplication = Button(buttons, bg="red", text="x", width=7, height=1, command=self.multiplication, relief=RAISED)
    Multiplication.grid(padx=2, pady=2, column=3, row=1)
    Division = Button(buttons, bg="powder blue", text="/", width=7, height=1, command=self.division, relief=RAISED)
    Division.grid(padx=2, pady=2, column=4, row=1)
    Addition = Button(buttons, bg="yellow", text="+", width=7, height=1, command=self.addition, relief=RAISED)
    Addition.grid(padx=2, pady=2, column=3, row=2)
    Subtraction = Button(buttons, bg="green", text="-", width=7, height=1, command=self.subtraction, relief=RAISED)
    Subtraction.grid(padx=2, pady=2, column=4, row=2)

2 个答案:

答案 0 :(得分:1)

使用带参数的列表:

buttons = [
    dict(column=3, row=0, text="C", command=self.clear),
    dict(column=4, row=3, text="=", command=self.equals),
    dict(column=4, row=0, text="AC", command=self.all_clear),
    dict(column=2, row=3, text="(", command=self.bracket_one),
    dict(column=3, row=3, text=")", command=self.bracket_two),
]
for data in buttons:
    button = Button(buttons, bg="snow", text=data['text'], width=7, height=1, command=data['command'], relief=RAISED)
    button.grid(padx=2, pady=2, column=data['column'], row=data['row'])

答案 1 :(得分:0)

作为Daniel解决方案的修改替代方案:

buttons = [
    ({'text':"C", 'command':self.clear}, {'column':3, 'row':0}),
    ({'text':"=", 'command':self.equals}, {'column':4, 'row':3}),
    ({'text':"AC", 'command':self.all_clear}, {'column':4, 'row':0}),
    ({'text':"(", 'command':self.bracket_one}, {'column':2, 'row':3}),
    ({'text':")", 'command':self.bracket_two}, {'column':3, 'row':3})
]
for data in buttons:
    button = Button(buttons, width=7, height=1, relief=RAISED, **data[0])
    button.grid(padx=2, pady=2, **data[1])

这将获取相应的字典并将其内容解压缩为关键字参数。这样做的一个好处是,您可以将bitmap添加到其中一个按钮,而无需将其中一个添加到其他按钮。