基本信息
源码名称:python 简易计算器 实例源码
源码大小:2.91KB
文件格式:.py
开发语言:Python
更新时间:2014-12-24
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
简易计算器
简易计算器
# -*- encoding: utf-8 -*-
# please input your code here.
from tkinter import *
def clear(): #定义函数清空表达式
display.set('')
def nExp(num): #定义函数将新字符添加进表达式
exp=display.get() num
display.set(exp)
def calculate(): #定义函数计算表达式结果
try:
exp=display.get()
result=eval(exp) #调用内置函数
display.set(exp '=' str(result))
except:
display.set('Error!') #如果表达式不合法则提示错误
root=Tk() #绘制窗口
root.title('简易计算器') #给窗口命名
display=StringVar() #定义表达式为交互式对象
LabelA=Label(root,relief='sunken',borderwidth=3,anchor=SE) #定义显示框并定义其属性
LabelA.configure(background='white',height=2,width=25) #设置计算器的背景颜色、长、宽
LabelA['textvariable']=display #将Label的内容与display关联
LabelA.grid(row=0,column=0,columnspan=4) #定位Label
#以下语句分别布置各按钮,并给出大小、位置以及触发的操作
Button(root,text='AC',width=5,height=3,command=clear).grid(row=1,rowspan=2)
#。。。。
Button(root,text='0',width=12,command=lambda:nExp('0')).grid(row=6,column=0,columnspan=2)
Button(root,text='.',width=5,command=lambda:nExp('.')).grid(row=6,column=2)
root.mainloop() #显示界面