import tkinter as tk import time window = tk.Tk() window.title("简易计算器") window.geometry('300x400') content = '' def btn_onclick(data): global content if data=="AC" or data =="MC": expression.set('') result.set('') content ='' elif data == '=': result.set(f'{round(eval(content),5)}') else: content+=data expression.set(content) #___________________________________________ lable_time_text = tk.Label(window,text="您好,北京时间:",font=("楷体",15)) lable_time_text.place(x=0,y=100,width=180,height=30) gettime = tk.StringVar(value=(time.strftime("%H:%M:%S",time.localtime(time.time())))) lable_time = tk.Label(window,textvariable=gettime,font=("楷体",15)) lable_time.place(x=180,y=100,width=100,height=30) #_____________________________________________ btn_datas =[ ["AC"," ","/","%"], ["7","8","9","*"], ["4","5","6","-"], ["1","2","3","+"], ["MC","0",".","="] ] # command=lambda x=btn_datas[r][c]:btn_onclick(x) for r in range(5): for c in range(4): btn_submit=tk.Button(window,text=btn_datas[r][c],font=("楷体",15),command=lambda x=btn_datas[r][c]:btn_onclick(x)) btn_submit.place(x=c*72+6,y=r*50+140,width=70,height=50) #------------------------------------------------ frame = tk.Frame(window,bd=2,relief="sunken") frame.place(x=10,y=10,width=270,height=30) expression = tk.StringVar( ) lable_express_text = tk.Label(frame,textvariable=expression,font=("楷体",15)) lable_express_text.place(x=0,y=0,width=270,height=30) lable_result_text = tk.Label(window,text="计算结果:",font=("楷体",15)) lable_result_text.place(x=0,y=50,width=150,height=30) result = tk.StringVar(value='0') lable_result_content = tk.Label(window,textvariable=result,font=("楷体",15)) lable_result_content.place(x=150,y=50,width=100,height=30) #______________________________________________________ window.mainloop()