import tkinter import tkinter.font
class Calculator(object): def __init__(self): self.root = tkinter.Tk() self.all_press_lists = [] self.is_press_compute = False self.result = tkinter.StringVar() self.record = tkinter.StringVar()
def main(self): self.root.minsize(300, 550) self.root.title('计算器') self.root.resizable(False,False)
input_bg, num_fg, btn_fg, btn_bg = "#ffffff", "#000000", "#000000", "#ffffff" btn_w, btn_h = 75, 70
my_font = tkinter.font.Font(family='微软雅黑', size=20) self.result.set(0) self.record.set('') label = tkinter.Label(self.root, font=my_font, bg=input_bg, bd='9', fg=num_fg, anchor='se', textvariable=self.record) label.place(width=300, height=120) label2 = tkinter.Label(self.root, font=my_font, bg=input_bg, bd='9', fg=num_fg, anchor='se', textvariable=self.result) label2.place(y=120, width=300, height=80)
btn_ac = tkinter.Button(self.root, text='c', font=my_font, bg=btn_bg, fg=btn_fg, bd=0, command=lambda: self.press_compute('AC')) btn_ac.place(x=btn_w * 0, y=200 + btn_h * 0, width=btn_w, height=btn_h) btn_back = tkinter.Button(self.root, text='←', font=my_font, bg=btn_bg, fg=btn_fg, bd=0, command=lambda: self.press_compute('b')) btn_back.place(x=btn_w * 1, y=200 + btn_h * 0, width=btn_w, height=btn_h) btn_per = tkinter.Button(self.root, text='%', font=my_font, bg=btn_bg, fg=btn_fg, bd=0, command=lambda: self.press_compute('%')) btn_per.place(x=btn_w * 2, y=200 + btn_h * 0, width=btn_w, height=btn_h) btn_divi = tkinter.Button(self.root, text='÷', font=my_font, bg=btn_bg, fg=btn_fg, bd=0, command=lambda: self.press_compute('/')) btn_divi.place(x=btn_w * 3, y=200 + btn_h * 0, width=btn_w, height=btn_h)
btn7 = tkinter.Button(self.root, text='7', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('7')) btn7.place(x=btn_w * 0, y=200 + btn_h * 1, width=btn_w, height=btn_h) btn8 = tkinter.Button(self.root, text='8', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('8')) btn8.place(x=btn_w * 1, y=200 + btn_h * 1, width=btn_w, height=btn_h) btn9 = tkinter.Button(self.root, text='9', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('9')) btn9.place(x=btn_w * 2, y=200 + btn_h * 1, width=btn_w, height=btn_h) btn_mul = tkinter.Button(self.root, text='×', font=my_font, bg=btn_bg, fg=btn_fg, bd=0, command=lambda: self.press_compute('*')) btn_mul.place(x=btn_w * 3, y=200 + btn_h * 1, width=btn_w, height=btn_h)
btn4 = tkinter.Button(self.root, text='4', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('4')) btn4.place(x=btn_w * 0, y=200 + btn_h * 2, width=btn_w, height=btn_h) btn5 = tkinter.Button(self.root, text='5', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('5')) btn5.place(x=btn_w * 1, y=200 + btn_h * 2, width=btn_w, height=btn_h) btn6 = tkinter.Button(self.root, text='6', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('6')) btn6.place(x=btn_w * 2, y=200 + btn_h * 2, width=btn_w, height=btn_h) btn_sub = tkinter.Button(self.root, text='-', font=my_font, bg=btn_bg, fg=btn_fg, bd=0, command=lambda: self.press_compute('-')) btn_sub.place(x=btn_w * 3, y=200 + btn_h * 2, width=btn_w, height=btn_h)
btn1 = tkinter.Button(self.root, text='1', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('1')) btn1.place(x=btn_w * 0, y=200 + btn_h * 3, width=btn_w, height=btn_h) btn2 = tkinter.Button(self.root, text='2', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('2')) btn2.place(x=btn_w * 1, y=200 + btn_h * 3, width=btn_w, height=btn_h) btn3 = tkinter.Button(self.root, text='3', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('3')) btn3.place(x=btn_w * 2, y=200 + btn_h * 3, width=btn_w, height=btn_h) btn_add = tkinter.Button(self.root, text='+', font=my_font, bg=btn_bg, fg=btn_fg, bd=0, command=lambda: self.press_compute('+')) btn_add.place(x=btn_w * 3, y=200 + btn_h * 3, width=btn_w, height=btn_h)
btn0 = tkinter.Button(self.root, text='0', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('0')) btn0.place(x=btn_w * 0, y=200 + btn_h * 4, width=btn_w * 2, height=btn_h) btn_point = tkinter.Button(self.root, text='.', font=my_font, bg=btn_bg, fg=num_fg, bd=0, command=lambda: self.press_num('.')) btn_point.place(x=btn_w * 2, y=200 + btn_h * 4, width=btn_w, height=btn_h) btn_equ = tkinter.Button(self.root, text='=', bg='#982425', font=my_font, fg=num_fg, bd=0, command=lambda: self.press_equal()) btn_equ.place(x=btn_w * 3, y=200 + btn_h * 4, width=btn_w, height=btn_h) self.root.mainloop()
def press_num(self, num): if self.is_press_compute is True: self.result.set(0) self.is_press_compute = False old_num = self.result.get() if old_num == '0': self.result.set(num) else: new_num = old_num + num self.result.set(new_num)
def press_compute(self, sign): num = self.result.get() self.all_press_lists.append(num) self.all_press_lists.append(sign) self.is_press_compute = True
if sign == 'AC': self.all_press_lists.clear() self.result.set(0) if sign == 'b': a = num[0:-1] self.all_press_lists.clear() self.result.set(a)
def press_equal(self): cur_num = self.result.get() self.all_press_lists.append(cur_num) compute_str = ''.join(self.all_press_lists) try: calculate_result = eval(compute_str) except: calculate_result = 'bad parameter' self.result.set(calculate_result) self.record.set(compute_str + "=") self.all_press_lists.clear()
if __name__ == '__main__': my_calculator = Calculator() my_calculator.main()
|