面向对象简易计算器

Posted 鹿港小镇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象简易计算器相关的知识,希望对你有一定的参考价值。

import tkinter
import math
import tkinter.messagebox

class jsq:


#界面布局方法
def __init__(self):
#创建界面
self.root = tkinter.Tk()
self.root.minsize(280,420)
self.root.title(\'chy计算器\')
#调用方法
self.showlabel()
self.myfunc()
self.root.resizable(width = False, height = False)

self.root.mainloop()


def myfunc(self):
print(\'执行操作\')

#添加菜单
#创建总菜单
allmenu = tkinter.Menu(self.root)
#创建查看菜单
filemenu= tkinter.Menu(allmenu,tearoff=0)
#添加查看子菜单
filemenu.add_command(label=\'标准型(T) Alt+1\',command = self.myfunc)
filemenu.add_command(label=\'科学型(S) Alt+2\',command = self.myfunc)
filemenu.add_command(label=\'程序员(P) Alt+3\',command = self.myfunc)
filemenu.add_command(label=\'统计信息(A) Alt+4\',command = self.myfunc)
#分割线
filemenu.add_separator()
filemenu.add_command(label=\'历史记录(Y) Ctrl+H\',command = self.myfunc)
filemenu.add_command(label=\'数字分组(I)\',command = self.myfunc)
#分割线
filemenu.add_separator()
filemenu.add_command(label=\'基本(B) Ctrl+F\',command = self.myfunc)
filemenu.add_command(label=\'单位转换(U) Ctrl+U\',command = self.myfunc)
filemenu.add_command(label=\'日期计算(D) Ctrl+E\',command = self.myfunc)
#分割线
filemen= tkinter.Menu(allmenu,tearoff=0)
filemenu.add_cascade(label=\'工作表(W)\',menu =filemen)
#创建工作表下的子菜单
filemen.add_command(label=\'抵押(M)\',command = self.myfunc)
filemen.add_command(label=\'汽车租赁(D)\',command = self.myfunc)
filemen.add_command(label=\'油耗(mpg)(F)\',command = self.myfunc)
filemen.add_command(label=\'油耗(U)\',command = self.myfunc)
#将查看菜单放入总菜单中
allmenu.add_cascade(label=\'查看(V)\',menu=filemenu)

#创建编辑总菜单
editmenu= tkinter.Menu(allmenu,tearoff=0)
editmenu.add_command(label=\'复制(C) Ctrl+C\',command= self.myfunc)
editmenu.add_command(label=\'粘贴(P) Ctrl+V\',command= self.myfunc)
#分割线
editmenu.add_separator()
editmenu.add_command(label=\'历史记录(H)\',command = self.myfunc)
#将编辑菜单加入总菜单
allmenu.add_cascade(label=\'编辑(E)\',menu=editmenu)

#创建帮助菜单
helpmenu= tkinter.Menu(allmenu,tearoff=0)
helpmenu.add_command(label=\'查看帮助(V) Ctrl+F1\',command = self.myfunc)
#分割线
helpmenu.add_separator()
helpmenu.add_command(label=\'关于计算器(A)\',command = self.myfunc)
#将帮总菜单助菜单加入
allmenu.add_cascade(label=\'帮助(H)\',menu=helpmenu)

#摆放菜单
self.root.config(menu = allmenu)

# 设置一个全局变量 运算数字和f符号的列表
lists = []
# 添加一个用于判断是否按下运算符号的标志
isPressSign = False
# 添加一个等于号的标志
pressEqual = False
#操作函数
#数字函数
def pressNum(self,num):
#全局变量
global lists
global isPressSign

# 判断是否按下了运算符号
if self.isPressSign == False:
pass
else:
self.result.set(0)
# 重置运算符号的状态
self.isPressSign = False

# 获取面板中的原有数字
oldnum = self.result.get()
# 判断界面数字是否为0
if oldnum == \'0\':
self.result.set(num)
else:
# 链接上 新按下的数字
newnum = oldnum + num
# 将按下的数字写到面板中
self.result.set(newnum)


# 运算函数
def pressCompute(self,sign):

# 保存已经按下的数字和运算符号
# 获取界面数字
num = self.result.get()
self.lists.append(num)

# 保存按下的操作符号
self.lists.append(sign)

# 设置运算符号为按下状态)
self.isPressSign = True


# 获取运算结果
def pressEqual(self):
# 全局化操作
global lists

# 获取所有的列表中的内容(之前的数字和操作)

# 获取当前界面尚的数字
curnum = self.result.get()
# 将当前界面的数数字存入列表
self.lists.append(curnum)
# 将列表转化为字符串
computeStr = \'\'.join(self.lists)
# 使用eval执行字符串中的运算即可
endNum = eval(computeStr)

# 将运算结果显式到界面中
self.result.set(endNum)
# 清空运算列表
self.lists.clear()

#MC键
def pressMc(self):
tkinter.messagebox.showerror(title=\'MC键\',message=\'无法定义按键\')

#MR键
def pressMr(self):
tkinter.messagebox.showerror(title=\'MR键\',message=\'无法定义按键\')

#MS键
def pressMs(self):
tkinter.messagebox.showerror(title=\'MS键\',message=\'无法定义按键\')

#M+键
def pressAdd(self):
tkinter.messagebox.showerror(title=\'M+键\',message=\'无法定义按键\')

#M-键
def pressSub(self):
tkinter.messagebox.showerror(title=\'M-键\',message=\'无法定义按键\')

#删除键(←)
def pressDelete(self):
#获取当前数的长度
len1 = len(self.result.get())
#判断长度是否大于1
if len1 > 1:
#每次执行按键功能,取片去除最后一位数,再输出显示
num1 = self.result.get()
num1 = num1[:len1-1]
self.result.set(num1)

# 清除键(CE)
def pressCE(self):
self.result.set(0)

#清除键(C)
def pressClear(self):
self.result.set(0)

#正负号键(±)
def pressSign(self):
# 获取当前的数,乘-1后输出到当前显示
if float(self.result.get()) == 0:
self.result.set(0)
else:
self.result.set(-1*float(self.result.get()))

#根号键(√)
def pressSqrt(self):
#获取当前的数,根平方后输出到当前显示
if float(self.result.get()) <= 0:
self.result.set(\'请输入大于0的整数\')
else:
self.result.set(math.sqrt(float(self.result.get())))

 

#倒数键(1/x)
def pressRec(self):
if float(self.result.get()) == 0:
self.result.set(0)
else:
self.result.set(1/float(self.result.get()))

 

#小数点键(.)
def pressDec(self,Del):
# 判断小数点是否被按下
if \'.\' in self.result.get():
pass
else:
self.result.set(self.result.get() + Del)

# 判断等号是否被按下
if self.pressEqual == True:
self.result.set(\'0\' + Del)
# 重置运算符状态
self.pressEqual = False

# 判断运算符是否被按下
if self.isPressSign == True:
self.result.set(\'0\' + Del)
#重置运算符状态
self.isPressSign = False

def showlabel(self):
#界面布局
#按钮show
self.result = tkinter.StringVar()
self.result.set(0)

show = tkinter.Label(self.root,textvariable=self.result,bg = \'white\',font = (\'黑体\',20),anchor = \'e\')
show.place(x = 5, y = 5,width = 270)

#第一行
btn = tkinter.Button(self.root,text = \'MC\',command = self.pressMc)
btn.place(x = 5, y = 60, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'MR\',command = self.pressMr)
btn.place(x = 60, y = 60, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'MS\',command = self.pressMs)
btn.place(x = 115, y = 60, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'M+\',command = self.pressAdd)
btn.place(x = 170, y = 60, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'M-\',command = self.pressSub)
btn.place(x = 225, y = 60, width = 50, height = 50)

#第二行
btn = tkinter.Button(self.root,text = \'←\',command = self.pressDelete)
btn.place(x = 5, y = 120, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'CE\',command= self.pressCE)
btn.place(x = 60, y = 120, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'C\',command = self.pressClear)
btn.place(x = 115, y = 120, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'±\',command= self.pressSign)
btn.place(x = 170, y = 120, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'√\',command = self.pressSqrt)
btn.place(x = 225, y = 120, width = 50, height = 50)

#第三行
btn7 = tkinter.Button(self.root,text = 7,command = lambda : self.pressNum(\'7\'))
btn7.place(x = 5, y = 180, width = 50, height = 50)

btn8 = tkinter.Button(self.root,text = 8,command = lambda : self.pressNum(\'8\'))
btn8.place(x = 60, y = 180, width = 50, height = 50)

btn9 = tkinter.Button(self.root,text = 9,command = lambda : self.pressNum(\'9\'))
btn9.place(x = 115, y = 180, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'/\',command = lambda :self.pressCompute(\'/\'))
btn.place(x = 170, y = 180, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'%\',command =lambda:self.pressCompute(\'%\'))
btn.place(x = 225, y = 180, width = 50, height = 50)

#第四行
btn4 = tkinter.Button(self.root,text = 4,command = lambda : self.pressNum(\'4\'))
btn4.place(x = 5, y = 240, width = 50, height = 50)

btn5 = tkinter.Button(self.root,text = 5,command = lambda : self.pressNum(\'5\'))
btn5.place(x = 60, y = 240, width = 50, height = 50)

btn6 = tkinter.Button(self.root,text = 6,command = lambda : self.pressNum(\'6\'))
btn6.place(x = 115, y = 240, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'*\',command = lambda :self.pressCompute(\'*\'))
btn.place(x = 170, y = 240, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'1/x\',command = self.pressRec)
btn.place(x = 225, y = 240, width = 50, height = 50)

#第五行
btn1 = tkinter.Button(self.root,text = 1,command = lambda : self.pressNum(\'1\'))
btn1.place(x = 5, y = 300, width = 50, height = 50)

btn2 = tkinter.Button(self.root,text = 2,command = lambda : self.pressNum(\'2\'))
btn2.place(x = 60, y = 300, width = 50, height = 50)

btn3 = tkinter.Button(self.root,text = 3,command = lambda : self.pressNum(\'3\'))
btn3.place(x = 115, y = 300, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'-\',command = lambda :self.pressCompute(\'-\'))
btn.place(x = 170, y = 300, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'=\',command = self.pressEqual)
btn.place(x = 225, y = 300, width = 50, height = 110)

#第六行
btn0 = tkinter.Button(self.root,text = 0,command = lambda : self.pressNum(\'0\'))
btn0.place(x = 5, y = 360, width = 105, height = 50)

btn = tkinter.Button(self.root,text = \'.\',command =lambda: self.pressDec(\'.\'))
btn.place(x = 115, y = 360, width = 50, height = 50)

btn = tkinter.Button(self.root,text = \'+\',command = lambda :self.pressCompute(\'+\'))
btn.place(x = 170, y = 360, width = 50, height = 50)

calculator = jsq()

以上是关于面向对象简易计算器的主要内容,如果未能解决你的问题,请参考以下文章

简易面向对象编程

PHP面向对象简易验证码类

es5 面向对象简易总结

java面向对象,简易版,通熟易懂

35.Python面向对象元类:type()__metaclass__属性实现简易ORM框架

35.Python面向对象元类:type()__metaclass__属性实现简易ORM框架