代码:
#coding:utf-8
import tkinter as tk
# 新建一个主窗口
root = tk.Tk()
# 设置窗口的大小
root.geometry('480x300+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='#F0CBDB',width=8,padx=5,pady=5)
btn_eight = tk.Button(root,text='8',font=font_number,bg='#F0CBDB',width=8,padx=5,pady=5)
btn_nine = tk.Button(root,text='9',font=font_number,bg='#F0CBDB',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='#F0CBDB',width=8,padx=5,pady=5)
btn_five = tk.Button(root,text='5',font=font_number,bg='#F0CBDB',width=8,padx=5,pady=5)
btn_six = tk.Button(root,text='6',font=font_number,bg='#F0CBDB',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='#F0CBDB',width=8,padx=5,pady=5)
btn_two = tk.Button(root,text='2',font=font_number,bg='#F0CBDB',width=8,padx=5,pady=5)
btn_three = tk.Button(root,text='3',font=font_number,bg='#F0CBDB',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='#F0CBDB',width=18,padx=5,pady=5)
# btn_zero1 = tk.Button(root,text='0',font=font_number,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_zero1.grid(row=6,column=2)
btn_dot.grid(row=6,column=3)
btn_equal.grid(row=6,column=4)
def inputnumber(param):
# 定义global变量
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()