您的位置:首页 > 编程语言 > Python开发

python:利用tkinter实现的计算器源代码

2015-05-22 09:32 609 查看
# -*- 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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: