# -*- coding:gbk -*-
'''
计算器程序
'''
import tkinter as tk
from math import *
root=tk.Tk()
topString=tk.StringVar()
#最上面的表达式输入框,
topEntry=tk.Entry(root)
topEntry['width']=40
topEntry['textvariable']=topString
topEntry['width']=40
topEntry.grid(row=0,column=0,columnspan=7)
#建立按钮组
buttonCaptions=['x!','sqrt','%','C','/','*','<-',\
'sin','cos','tan','7','8','9','-',\
'log','log10','1/x','4','5','6','+',\
'e^x','x^2','y^x','1','2','3','(',\
'abs','pi','e','0','.',')','=']
### 绑定按钮事件
def edit(caption):
topString.set(topString.get()+ caption) #如何得到按钮的标题??
buttons={}
for i in range(5):
for j in range(7):
t=tk.Button(root,text=buttonCaptions[i*7+j])
t['width']=5
t['command']=lambda :edit(buttonCaptions[i*7+j])
t.grid(row=i+1,column=j)
buttons[buttonCaptions[i*7+j]]=t
buttons['sqrt']['command']=lambda : topString.set(topString.get()+'sqrt')
buttons['%']['command']=lambda : topString.set(topString.get()+'%')
buttons['/']['command']=lambda : topString.set(topString.get()+'/')
buttons['*']['command']=lambda : topString.set(topString.get()+'*')
buttons['sin']['command']=lambda : topString.set(topString.get()+'sin')
buttons['cos']['command']=lambda : topString.set(topString.get()+'cos')
buttons['tan']['command']=lambda : topString.set(topString.get()+'tan')
buttons['7']['command']=lambda : topString.set(topString.get()+'7')
buttons['8']['command']=lambda : topString.set(topString.get()+'8')
buttons['9']['command']=lambda : topString.set(topString.get()+'9')
buttons['-']['command']=lambda : topString.set(topString.get()+'-')
buttons['log']['command']=lambda : topString.set(topString.get()+'log')
buttons['log10']['command']=lambda : topString.set(topString.get()+'log10')
buttons['4']['command']=lambda : topString.set(topString.get()+'4')
buttons['5']['command']=lambda : topString.set(topString.get()+'5')
buttons['6']['command']=lambda : topString.set(topString.get()+'6')
buttons['+']['command']=lambda : topString.set(topString.get()+'+')
buttons['e^x']['command']=lambda : topString.set(topString.get()+'e**')
buttons['x^2']['command']=lambda : topString.set(topString.get()+'x**2')
buttons['y^x']['command']=lambda : topString.set(topString.get()+'**')
buttons['1']['command']=lambda : topString.set(topString.get()+'1')
buttons['2']['command']=lambda : topString.set(topString.get()+'2')
buttons['3']['command']=lambda : topString.set(topString.get()+'3')
buttons['(']['command']=lambda : topString.set(topString.get()+'(')
buttons['abs']['command']=lambda : topString.set(topString.get()+'abs')
buttons['pi']['command']=lambda : topString.set(topString.get()+'pi')
buttons['e']['command']=lambda : topString.set(topString.get()+'e')
buttons['0']['command']=lambda : topString.set(topString.get()+'0')
buttons['.']['command']=lambda : topString.set(topString.get()+'.')
buttons[')']['command']=lambda : topString.set(topString.get()+')')
#清除按钮动作
def clear():
topString.set('')
buttons['C']['command']=clear
#退格按钮动作
def back():
topString.set(topString.get()[:-1])
buttons['<-']['command']=back
#绑定计算按钮的事件处理
def calculate():
topString.set(eval(topString.get()))
buttons['=']['command']=calculate
##buttonCaptions=['x!','1/x'] #未设置动作的按钮
root.mainloop()