python编写计算器程序-python代码编写计算器⼩程序本⽂实例为⼤家分享了python计算器⼩程序的具体代码,供⼤家参考,具体内容如下
import tkinter
ssagebox
import math
class JSQ:
def __init__(self):
#创建主界⾯
槽罐车是什么车
< = tkinter.Tk()
#定义⼀个变量赋值给页⾯label
火花塞品牌#界⾯先显⽰个0
# 设置⼀个全局变量 运算数字和符号的列表
self.lists = []
# 添加⼀个判断是否按下运算符号的标志
self.isPressSign = False
# 添加⼀个判断是否运算完毕的标志,如果运算完毕在获取数字的⽅法中会清空上⼀轮运算结果
self.islistsclear = False
# 添加⼀个判断百分号是否使⽤的功能
self.isbaifenhao = False
# 为了下⾯的百分号⽅法使⽤(在百分号中为按下运算符号之后的数字)
self.num1 = ''
# 定义⼀个全局变量(获取百分好的结果)为了在"='运算的时候输出
# 获取运算符号之前的数字
self.num = ''
# 获取运算符号给百分好⽅法使⽤
self.sign1 = ''
#将窗⼝⼀直显⽰
def menubar(self):
# 创建总菜单
allmenu = tkinter.)
# 创建⼦菜单
filemenu = tkinter.Menu(allmenu, tearoff=0)
# 在⼦菜单⾥添加⽂件
filemenu.add_command(label='标准型(T) Alt+1',command = lambda : self.func2()) filemenu.add_command(label='科学性(S) Alt+2',command = lambda : self.func2()) filemenu.add_command(label='程序员(p) Alt+3',command = lambda : self.func2()) filemenu.add_command(label='统计信息(A) Alt+4',command = lambda : self.func2())
# 创建分割线
filemenu.add_separator()
filemenu.add_command(label='历史记录(Y) Ctrl+H',command = lambda : self.func2()) filemenu.add_command(label='数学分组(I)',command = lambda : self.func2())
filemenu.add_separator()
filemenu.add_command(label='基本(B) Ctrl+F4',command = lambda : self.func2()) filemenu.add_command(label='单位转换(U) Ctrl+U',command = lambda : self.func2()) filemenu.add_command(label='⽇期计算(D) Ctrl+E',command = lambda : self.func2())
# 在⼦菜单⾥再创建⼀个菜单
filemenunei = tkinter.Menu(filemenu, tearoff=0)
# 在⼦菜单⾥的菜单添加⽂件
filemenunei.add_command(label='抵押(M)',command = lambda : self.func2()) filemenunei.add_command(label='汽车租赁(V)',command = lambda : self.func2()) filemenunei.add_command(label='油耗(mpg)(F)',command = lambda : self.func2()) filemenunei.add_command(label='油耗(1/100km)(U)',command = lambda : self.func2()) # 将⼦菜单⾥的菜单添加到⼦菜单⾥
filemenu.add_cascade(label='⼯作表(W)', menu=filemenunei)
# 添加退出功能
filemenu.add_command(label='退出', quit)
# 将⼦菜单添加到总菜单⾥
editmenu = tkinter.Menu(allmenu, tearoff=0)
# 在⼦菜单⾥添加⽂件
editmenu.add_command(label='复制(C)',command = lambda : self.func2())
editmenu.add_command(label='粘贴(P)',command = lambda : self.func2())
# 创建分割线
editmenu.add_separator()
# 在⼦菜单⾥创建⼀个菜单
editmenunei = tkinter.Menu(editmenu, tearoff=0)
# 在⼦菜单⾥的菜单添加⽂件
editmenunei.add_command(label='复制历史记录(I)', stat='disable')
editmenunei.add_command(label='编辑(E) F2', stat='disable')
editmenunei.add_command(label='取消编辑(N) Esc', stat='disable')
editmenunei.add_command(label='清除(L) Ctrl+Shift+D', stat='disable')
宝马730i# 将⼦菜单⾥的菜单添加到⼦菜单
editmenu.add_cascade(label='历史记录(H)', menu=editmenunei)
# 将⼦菜单添加到总菜单
allmenu.add_cascade(label='编辑(E)', menu=editmenu)
# 创建第三个⼦菜单
4月17油价上涨还是下调
helpmenu = tkinter.Menu(allmenu, tearoff=0)
# 在第三个⼦菜单⾥添加⽂件
helpmenu.add_command(label='查看帮助(v) F1',command = lambda : self.func2())
# 创建分割线
helpmenu.add_separator()
helpmenu.add_command(label='关于计算器(A)',command = lambda : self.func2())
# 将第三个⼦菜单添加到总菜单中
allmenu.add_cascade(label='帮助(H)', menu=helpmenu)
# 将总菜单布局到主窗⼝
# 下⾯代码⽤于页⾯布局
def layout(self):
#显⽰结果的标签
label = tkinter., sult, bg='white', font=('⿊体', 10), anchor='e')
btnmr = tkinter., text='MR',command = lambda : self.func2()) btnmr.place(x=70, y=50, width=30, height=30)
btnms = tkinter., text='MS',command = lambda : self.func2()) btnms.place(x=120, y=50, width=30, height=30)
btnmjia = tkinter., text='M+',command = lambda : self.func2()) btnmjia.place(x=170, y=50, width=30, height=30)
btnmjian = tkinter., text='M-',command = lambda : self.func2()) btnmjian.place(x=220, y=50, width=30, height=30)
btnjiantou = tkinter., text='←', command=lambda: self.jiantou()) btnjiantou.place(x=20, y=90, width=30, height=30)
btnce = tkinter., text='CE', command=lambda: self.CE())
btnce.place(x=70, y=90, width=30, height=30)
btnc = tkinter., text='C', command=lambda: self.clears())
btnc.place(x=120, y=90, width=30, height=30)
btnzhengfu = tkinter., text='±', command=lambda: self.zhenffu()) btnzhengfu.place(x=170, y=90, width=30, height=30)
btnkaigen = tkinter., text='√', command=lambda: self.sqrts()) btnkaigen.place(x=220, y=90, width=30, height=30)
btn7 = tkinter., text='7', command=lambda: self.pressNum('7'))
呼和浩特车管所考试中心
btn7.place(x=20, y=130, width=30, height=30)
btn8 = tkinter., text='8', command=lambda: self.pressNum('8'))
btn8.place(x=70, y=130, width=30, height=30)
btn9 = tkinter., text='9', command=lambda: self.pressNum('9'))
btn9.place(x=120, y=130, width=30, height=30)
btnchu = tkinter., text='/', command=lambda: self.pressCompute('/')) btnchu.place(x=170, y=130, width=30, height=30)
btnbaifen = tkinter., text='%', command=lambda: self.baifenhao()) btnbaifen.place(x=220, y=130, width=30, height=30)
btn4 = tkinter., text='4', command=lambda: self.pressNum('4'))
btn4.place(x=20, y=170, width=30, height=30)
btn5 = tkinter., text='5', command=lambda: self.pressNum('5'))
btncheng = tkinter., text='*', command=lambda: self.pressCompute('*')) btncheng.place(x=170, y=170, width=30, height=30)
btnfenshu = tkinter., text='1/X', command=lambda: self.fenshu()) btnfenshu.place(x=220, y=170, width=30, height=30)
btn1 = tkinter., text='1', command=lambda: self.pressNum('1'))
btn1.place(x=20, y=210, width=30, height=30)
btn2 = tkinter., text='2', command=lambda: self.pressNum('2'))
btn2.place(x=70, y=210, width=30, height=30)
btn3 = tkinter., text='3', command=lambda: self.pressNum('3'))汽车计算器
btn3.place(x=120, y=210, width=30, height=30)
btnjian = tkinter., text='-', command=lambda: self.pressCompute('-')) btnjian.place(x=170, y=210, width=30, height=30)
btndenghao = tkinter., text='=', command=lambda: self.pressEqual()) btndenghao.place(x=220, y=210, width=30, height=70)
btn0 = tkinter., text='0', command=lambda: self.pressNum('0'))
btn0.place(x=20, y=250, width=80, height=30)
btndian = tkinter., text='.', command=lambda: self.pressNum('.')) btndian.place(x=120, y=250, width=30, height=30)
btnjia = tkinter., text='+', command=lambda: self.pressCompute('+')) btnjia.place(x=170, y=250, width=30, height=30)
# 数字函数(获取数字的函数)
def pressNum(self,num):
# 下⾯的百分好会⽤到
self.num1 = num
# 判断是否按下运算符号
if self.isPressSign == False:
pass
#如果按下运算符号界⾯归0
else:
self.isPressSign = False