1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| import tkinter as tk
root = tk.Tk()
root.geometry('462x300+100+100')
root.title("计算器")
result_str= tk.StringVar() result_str.set('') label = tk.Label(root,textvariable=result_str,font=('宋体',20),width=32,padx=5, pady=5,anchor='e') label.grid(row=1,column=1,columnspan=4) font_number = ('宋体',18) btn_m = tk.Button(root,text='%',font=font_number,width=8,padx=5,pady=5) btn_CE = tk.Button(root,text='CE',font=font_number,width=8,padx=5,pady=5) btn_clear = tk.Button(root,text='C',font=font_number,width=8,padx=5,pady=5) btn_div = tk.Button(root,text='/',font=font_number,width=8,padx=5,pady=5) btn_m.grid(row=2,column=1) btn_CE.grid(row=2,column=2) btn_clear.grid(row=2,column=3) btn_div.grid(row=2,column=4) font_number = ('宋体',18) btn_seven = tk.Button(root,text='7',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_eight = tk.Button(root,text='8',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_nine = tk.Button(root,text='9',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_multi = tk.Button(root,text='x',font=font_number,width=8,padx=5,pady=5) btn_seven.grid(row=3,column=1) btn_eight.grid(row=3,column=2) btn_nine.grid(row=3,column=3) btn_multi.grid(row=3,column=4)
btn_four = tk.Button(root,text='4',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_five = tk.Button(root,text='5',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_six = tk.Button(root,text='6',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_sub = tk.Button(root,text='-',font=font_number,width=8,padx=5,pady=5) btn_four.grid(row=4,column=1) btn_five.grid(row=4,column=2) btn_six.grid(row=4,column=3) btn_sub.grid(row=4,column=4) btn_one = tk.Button(root,text='1',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_two = tk.Button(root,text='2',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_three = tk.Button(root,text='3',font=font_number,bg='#FFFFFF',width=8,padx=5,pady=5) btn_add = tk.Button(root,text='+',font=font_number,width=8,padx=5,pady=5) btn_one.grid(row=5,column=1) btn_two.grid(row=5,column=2) btn_three.grid(row=5,column=3) btn_add.grid(row=5,column=4)
btn_zero = tk.Button(root,text='0',font=font_number,bg='#FFFFFF',width=18,padx=5,pady=5) btn_dot = tk.Button(root,text='.',font=font_number,width=8,padx=5,pady=5) btn_equal = tk.Button(root,text='=',font=font_number,width=8,padx=5,pady=5) btn_zero.grid(row=6,column=1,columnspan=2) btn_dot.grid(row=6,column=3) btn_equal.grid(row=6,column=4)
def inputnumber(param): result_str.set(result_str.get() + param) def equal(): str = result_str.get() if len(str) != 0: num = eval(str) result_str.set(num) def clear(): result_str.set('') btn_zero.config(command=lambda:inputnumber('0') ); btn_one.config(command=lambda:inputnumber('1') ); btn_two.config(command=lambda:inputnumber('2')); btn_three.config(command=lambda:inputnumber('3')); btn_four.config(command=lambda:inputnumber('4')); btn_five.config(command=lambda:inputnumber('5')); btn_six.config(command=lambda:inputnumber('6')); btn_seven.config(command=lambda:inputnumber('7')); btn_eight.config(command=lambda:inputnumber('8')); btn_nine.config(command=lambda:inputnumber('9')); btn_add.config(command=lambda:inputnumber('+')); btn_sub.config(command=lambda:inputnumber('-')); btn_multi.config(command=lambda:inputnumber('*')); btn_div.config(command=lambda:inputnumber('/')); btn_dot.config(command=lambda:inputnumber('.'));
btn_equal.config(command=equal)
btn_clear.config(command=clear)
root.mainloop()
|