课堂笔记 ---- 面向对象计算器

import tkinter import math class objc: # 界面布局方法 def __init__(self): # 创建主界面 并且保存到成员属性中 self.root = tkinter.Tk() self.root.minsize(300, 400) self.root.title('计算器') self.root.minsize(300, 420) self.root.maxsize(300, 420) # 设置显式面板的变量 self.rst = tkinter.StringVar() self.rst.set(0) # 设置一个全局变量 运算数字和f符号的列表 self.lists = [] # 添加一个用于判断是否按下运算符号的标志 self.isPressSign = False # 界面布局 self.Toast() self.root.mainloop() def Toast(self): # 显示面板 lable = tkinter.Label(self.root, textvariable=self.rst, width=22, height=2, bg='white', font=('黑体', 15, 'bold'),anchor='se') lable.place(x=15, y=15) # 数字 btn1 = tkinter.Button(self.root, text='1', command=lambda: self.pressNum('1')) btn1.place(width=48, height=40, x=15, y=285) btn2 = tkinter.Button(self.root, text='2', command=lambda: self.pressNum('2')) btn2.place(width=48, height=40, x=71, y=285) btn3 = tkinter.Button(self.root, text='3', command=lambda: self.pressNum('3')) btn3.place(width=48, height=40, x=127, y=285) btn4 = tkinter.Button(self.root, text='4', command=lambda: self.pressNum('4')) btn4.place(width=48, height=40, x=15, y=240) btn5 = tkinter.Button(self.root, text='5', command=lambda: self.pressNum('5')) btn5.place(width=48, height=40, x=71, y=240) btn6 = tkinter.Button(self.root, text='6', command=lambda: self.pressNum('6')) btn6.place(width=48, height=40, x=127, y=240) btn7 = tkinter.Button(self.root, text='7', command=lambda: self.pressNum('7')) btn7.place(width=48, height=40, x=15, y=195) btn8 = tkinter.Button(self.root, text='8', command=lambda: self.pressNum('8')) btn8.place(width=48, height=40, x=71, y=195) btn9 = tkinter.Button(self.root, text='9', command=lambda: self.pressNum('9')) btn9.place(width=48, height=40, x=127, y=195) btn0 = tkinter.Button(self.root,text = '0',command = lambda: self.pressNum('0')) btn0.place(width=105,height = 40,x=15,y=330) # 运算符号 btnd = tkinter.Button(self.root, text='.', command=lambda: self.pressNum('.')) btnd.place(width=48, height=40, x=127, y=330) btnj = tkinter.Button(self.root, text='+', command=lambda: self.pressCompute('+')) btnj.place(width=48, height=40, x=183, y=330) btnji = tkinter.Button(self.root, text='-', command=lambda: self.pressCompute('-')) btnji.place(width=48, height=40, x=183, y=285) btnde = tkinter.Button(self.root, text='=', command=lambda: self.pressEqual()) btnde.place(width=48, height=85, x=239, y=285) btnx = tkinter.Button(self.root,text = '*',command = lambda: self.pressCompute('*')) btnx.place(width=48,height = 40,x=183,y=240) btnqf = tkinter.Button(self.root,text = '1/x',command = lambda: self.pressqf()) btnqf.place(width=48,height = 40,x=239,y=240) btnchu = tkinter.Button(self.root, text='/', command=lambda: self.pressCompute('/')) btnchu.place(width=48, height=40, x=183, y=195) btnb = tkinter.Button(self.root, text='%', command=lambda: self.pressb()) btnb.place(width=48, height=40, x=239, y=195) btnzuo = tkinter.Button(self.root, text='←', command=lambda: self.presszuo()) btnzuo.place(width=48, height=40, x=15, y=150) btnce = tkinter.Button(self.root, text='CE', command=lambda: self.pressce()) btnce.place(width=48, height=40, x=71, y=150) btnc = tkinter.Button(self.root, text='C', command=lambda: self.pressc()) btnc.place(width=48, height=40, x=127, y=150) btnzf = tkinter.Button(self.root, text='±', command=lambda: self.presszf()) btnzf.place(width=48, height=40, x=183, y=150) btngh = tkinter.Button(self.root, text='√', command=lambda: self.pressgh()) btngh.place(width=48, height=40, x=239, y=150) btnmc = tkinter.Button(self.root, text='MC', state='disable') btnmc.place(width=48, height=40, x=15, y=105) btnmr = tkinter.Button(self.root, text='MR', state='disable') btnmr.place(width=48, height=40, x=71, y=105) btnms = tkinter.Button(self.root, text='MS', state='disable') btnms.place(width=48, height=40, x=127, y=105) btnmmj = tkinter.Button(self.root, text='M+', state='disable') btnmmj.place(width=48, height=40, x=183, y=105) btnmmji = tkinter.Button(self.root, text='M-', state='disable') btnmmji.place(width=48, height=40, x=239, y=105) #数字函数 def pressNum(self, num): # 判断是否按下类运算符号 if self.isPressSign == False: pass else: self.rst.set(0) # 重置运算符号的状态 self.isPressSign = False # 获取原有面板中的数字 oldnum = self.rst.get() # 判断界面中数字是否为0 if oldnum == '0': if num == '.': self.rst.set('0' + num) else: self.rst.set(num) else: #判断是否输入过. if oldnum.find('.') != -1 and num == '.': pass else: # 将按下的数字写道面板上 newnum = oldnum + num self.rst.set(newnum) # 运算函数 def pressCompute(self,sign): # 保存已经按下的数字和运算符号 # 获取界面中的数字 # 保存按下的操作符号 if self.isPressSign == False: num = self.rst.get() self.lists.append(num) self.lists.append(sign) else: pass # 设置运算符号为按下状态 self.isPressSign = True # 获取运算结果 def pressEqual(self): # 获取当前界面上的数字 curnum = self.rst.get() # 将界面上的数字存入列表 self.lists.append(curnum) # 将列表转化为字符串 computeStr = ''.join(self.lists) # 使用eval执行字符串中的运行 endNum = eval(computeStr) # 将运行结果显示到界面中 self.rst.set(endNum) # 清空列表 self.lists.clear() # 清空列表 def pressc(self): self.lists.clear() self.rst.set(0) #清空当前界面 def pressce(self): self.rst.set(0) #正负号 def presszf(self): zf = self.rst.get() if zf[0] == '-': self.rst.set(zf[1:]) elif zf[0] != '-' and zf[0] != '0': self.rst.set('-' + zf) #删除当前界面 def presszuo(self): zuo = self.rst.get() newzuo = zuo[:-1] self.rst.set(newzuo) #百分号函数 def pressb(self): curnum = self.rst.get() # 将列表转化为字符串 computeStr = ''.join(self.lists) # 使用eval执行字符串中的运行 endNum = eval(computeStr[:-1]) # 判断列表是否为空 if self.lists != []: newper = str(float(endNum) * float(curnum) * 0.01) # 将字符串中的.0删除 if newper[-1] == '0': self.rst.set(newper[:-2]) else: self.rst.set(newper) #取反 def pressqf(self): qf = self.rst.get() qf = 1 / float(qf) newqf = str(qf) #将字符串中的.0删除 if newqf[-1] == '0': self.rst.set(newqf[:-2]) else: self.rst.set(newqf) #开平方 def pressgh(self): gh = self.rst.get() #开平方 gh = math.sqrt(float(gh)) newgh = str(gh) if newgh[-1] == '0': self.rst.set(newgh[:-2]) else: self.rst.set(newgh) #实例化对象 myobjc = objc()
posted @ 2017-12-01 19:21  miaomf  阅读(242)  评论(0编辑  收藏  举报