基于Python实现仿Windows标准计算器
文章背景
本人已完成仿Windows简易计算器的项目,就想着完成Windows标准计算器。最近,在知乎里发现用Python做一个计算器这篇文章。这篇文章内容就阐述了基于Python实现标准计算器,本人只需稍加修改就行。
成果展示
Windows标准计算器
项目成果图
项目最后成果虽然并不美观,但是和Windows标准计算器布局类似。往后,会完善这个项目。
项目结构
本人根据项目功能,分为
- Init.py为计算器初始化文件
- Function.py为计算器逻辑功能文件
- Style.py为计算器布局文件
- main.py为计算器启动文件
项目结构图
项目代码
Init.py
- import math
- import tkinter
- root = tkinter.Tk()
- root.resizable(width=False, height=False)
- #是否按下了运算符
- IS_CALC = False
- #存储数字
- STORAGE = []
- #显示框最多显示多少个字符
- MAXSHOWLEN = 18
- #当前显示的数字
- CurrentShow = tkinter.StringVar()
- CurrentShow.set('0')
Function.py
- from Init import *
- #按下数字键(0-9)#
- def pressNumber(number):
- global IS_CALC
- if IS_CALC:
- CurrentShow.set('0')
- IS_CALC = False
- if CurrentShow.get() == '0':
- CurrentShow.set(number)
- else:
- if len(CurrentShow.get()) < MAXSHOWLEN:
- CurrentShow.set(CurrentShow.get() + number)
-
-
- #按下小数点#
- def pressDP():
- global IS_CALC
- if IS_CALC:
- CurrentShow.set('0')
- IS_CALC = False
- if len(CurrentShow.get().split('.')) == 1:
- if len(CurrentShow.get()) < MAXSHOWLEN:
- CurrentShow.set(CurrentShow.get() + '.')
-
-
- #清零#
- def clearAll():
- global STORAGE
- global IS_CALC
- STORAGE.clear()
- IS_CALC = False
- CurrentShow.set('0')
-
-
- #清除当前显示框内所有数字#
- def clearCurrent():
- CurrentShow.set('0')
-
-
- #删除显示框内最后一个数字#
- def delOne():
- global IS_CALC
- if IS_CALC:
- CurrentShow.set('0')
- IS_CALC = False
- if CurrentShow.get() != '0':
- if len(CurrentShow.get()) > 1:
- CurrentShow.set(CurrentShow.get()[:-1])
- else:
- CurrentShow.set('0')
-
-
- #计算答案修正#
- def modifyResult(result):
- result = str(result)
- if len(result) > MAXSHOWLEN:
- if len(result.split('.')[0]) > MAXSHOWLEN:
- result = 'Overflow'
- else:
- # 直接舍去不考虑四舍五入问题
- result = result[:MAXSHOWLEN]
- return result
-
-
- #按下运算符#
- def pressOperator(operator):
- global STORAGE
- global IS_CALC
- if operator == '+/-':
- if CurrentShow.get().startswith('-'):
- CurrentShow.set(CurrentShow.get()[1:])
- else:
- CurrentShow.set('-'+CurrentShow.get())
- elif operator == '1/x':
- try:
- result = 1 / float(CurrentShow.get())
- except:
- result = 'illegal operation'
- result = modifyResult(result)
- CurrentShow.set(result)
- IS_CALC = True
- elif operator == 'sqrt':
- try:
- result = math.sqrt(float(CurrentShow.get()))
- except:
- result = 'illegal operation'
- result = modifyResult(result)
- CurrentShow.set(result)
- IS_CALC = True
- elif operator=='x²':
- result=int(math.pow(int(CurrentShow.get()),2))
- result = modifyResult(result)
- CurrentShow.set(result)
- IS_CALC = True
- elif operator == 'MC':
- STORAGE.clear()
- elif operator == 'MR':
- if IS_CALC:
- CurrentShow.set('0')
- STORAGE.append(CurrentShow.get())
- expression = ''.join(STORAGE)
- try:
- result = eval(expression)
- except:
- result = 'illegal operation'
- result = modifyResult(result)
- CurrentShow.set(result)
- IS_CALC = True
- elif operator == 'MS':
- STORAGE.clear()
- STORAGE.append(CurrentShow.get())
- elif operator == 'M+':
- STORAGE.append(CurrentShow.get())
- elif operator == 'M-':
- if CurrentShow.get().startswith('-'):
- STORAGE.append(CurrentShow.get())
- else:
- STORAGE.append('-' + CurrentShow.get())
- elif operator in ['+', '-', '*', '/', '%%']:
- STORAGE.append(CurrentShow.get())
- STORAGE.append(operator)
- IS_CALC = True
- elif operator == '=':
- if IS_CALC:
- CurrentShow.set('0')
- STORAGE.append(CurrentShow.get())
- expression = ''.join(STORAGE)
- try:
- result = eval(expression)
- # 除以0的情况
- except:
- result = 'illegal operation'
- result = modifyResult(result)
- CurrentShow.set(result)
- STORAGE.clear()
- IS_CALC = True
-
Style.py
- from Init import *
- from Function import *
- def Demo():
- root.minsize(320, 420)
- root.title('Calculator')
- # 布局
- # --文本框
- label = tkinter.Label(root, textvariable=CurrentShow, bg='black', anchor='e', bd=5, fg='white', font=('楷体', 20))
- label.place(x=20, y=50, width=280, height=50)
- # --第一行
- # ----Memory clear
- button1_1 = tkinter.Button(text='MC', bg='#666', bd=2, command=lambda:pressOperator('MC'))
- button1_1.place(x=20, y=110, width=50, height=35)
- # ----Memory read
- button1_2 = tkinter.Button(text='MR', bg='#666', bd=2, command=lambda:pressOperator('MR'))
- button1_2.place(x=77.5, y=110, width=50, height=35)
- # ----Memory +
- button1_3 = tkinter.Button(text='M+', bg='#666', bd=2, command=lambda:pressOperator('M+'))
- button1_3.place(x=135, y=110, width=50, height=35)
- # ----Memory -
- button1_4 = tkinter.Button(text='M-', bg='#666', bd=2, command=lambda:pressOperator('M-'))
- button1_4.place(x=192.5, y=110, width=50, height=35)
- # ----Memory save
- button1_5 = tkinter.Button(text='MS', bg='#666', bd=2, command=lambda: pressOperator('MS'))
- button1_5.place(x=250, y=110, width=50, height=35)
- # --第二行
- # ----取余
- button2_1 = tkinter.Button(text='%%', bg='#708069', bd=2, command=lambda:pressOperator('%%'))
- button2_1.place(x=20, y=155, width=62.5, height=35)
- # ----清除当前显示框内所有数字
- button2_2 = tkinter.Button(text='CE', bg='#666', bd=2, command=lambda:clearCurrent())
- button2_2.place(x=77.5, y=155, width=62.5, height=35)
- # ----清零(相当于重启)
- button2_3 = tkinter.Button(text='C', bg='#666', bd=2, command=lambda: clearAll())
- button2_3.place(x=135, y=155, width=62.5, height=35)
- # ----删除单个数字
- button2_4 = tkinter.Button(text='del', bg='#666', bd=2, command=lambda:delOne())
- button2_4.place(x=192.5, y=155, width=62.5, height=35)
- # --第三行
- # ----取导数
- button3_1 = tkinter.Button(text='1/x', bg='#708069', bd=2, command=lambda: pressOperator('1/x'))
- button3_1.place(x=20, y=190, width=62.5, height=35)
- # ----平方
- button3_2 = tkinter.Button(text='x²', bg='#708069', bd=2, command=lambda: pressOperator('x²'))
- button3_2.place(x=77.5, y=190, width=62.5, height=35)
- # ----开根号
- button3_3 = tkinter.Button(text='sqrt', bg='#666', bd=2, command=lambda:pressOperator('sqrt'))
- button3_3.place(x=135, y=190, width=62.5, height=35)
- # ----除
- button3_4 = tkinter.Button(text='/', bg='#708069', bd=2, command=lambda: pressOperator('/'))
- button3_4.place(x=192.5, y=190, width=62.5, height=35)
- # --第四行
- # ----7
- button4_1 = tkinter.Button(text='7', bg='#bbbbbb', bd=2, command=lambda:pressNumber('7'))
- button4_1.place(x=20, y=225, width=62.5, height=35)
- # ----8
- button4_2 = tkinter.Button(text='8', bg='#bbbbbb', bd=2, command=lambda:pressNumber('8'))
- button4_2.place(x=77.5, y=225, width=62.5, height=35)
- # ----9
- button4_3 = tkinter.Button(text='9', bg='#bbbbbb', bd=2, command=lambda:pressNumber('9'))
- button4_3.place(x=135, y=225, width=62.5, height=35)
- # ----乘
- button4_4 = tkinter.Button(text='*', bg='#708069', bd=2, command=lambda: pressOperator('*'))
- button4_4.place(x=192.5, y=225, width=62.5, height=35)
- # --第五行
- # ----4
- button5_1 = tkinter.Button(text='4', bg='#bbbbbb', bd=2, command=lambda:pressNumber('4'))
- button5_1.place(x=20, y=265, width=62.5, height=35)
- # ----5
- button5_2 = tkinter.Button(text='5', bg='#bbbbbb', bd=2, command=lambda:pressNumber('5'))
- button5_2.place(x=77.5, y=265, width=62.5, height=35)
- # ----6
- button5_3 = tkinter.Button(text='6', bg='#bbbbbb', bd=2, command=lambda:pressNumber('6'))
- button5_3.place(x=135, y=265, width=62.5, height=35)
- # ----减
- button5_4 = tkinter.Button(text='-', bg='#708069', bd=2, command=lambda: pressOperator('-'))
- button5_4.place(x=192.5, y=265, width=62.5, height=35)
- # --第六行
- # ----1
- button6_1 = tkinter.Button(text='1', bg='#bbbbbb', bd=2, command=lambda: pressNumber('1'))
- button6_1.place(x=20, y=300, width=62.5, height=35)
- # ----2
- button6_2 = tkinter.Button(text='2', bg='#bbbbbb', bd=2, command=lambda: pressNumber('2'))
- button6_2.place(x=77.5, y=300, width=62.5, height=35)
- # ----3
- button6_3 = tkinter.Button(text='3', bg='#bbbbbb', bd=2, command=lambda:pressNumber('3'))
- button6_3.place(x=135, y=300, width=62.5, height=35)
- # ----加
- button6_4 = tkinter.Button(text='+', bg='#708069', bd=2, command=lambda: pressOperator('+'))
- button6_4.place(x=192.5, y=300, width=62.5, height=35)
- # --第七行
- # ----0
- # ----取反
- button7_1 = tkinter.Button(text='+/-', bg='#666', bd=2, command=lambda: pressOperator('+/-'))
- button7_1.place(x=20, y=345, width=62.5, height=35)
- button7_2 = tkinter.Button(text='0', bg='#bbbbbb', bd=2, command=lambda:pressNumber('0'))
- button7_2.place(x=77.5, y=345, width=62.5, height=35)
- # ----小数点
- button7_3 = tkinter.Button(text='.', bg='#bbbbbb', bd=2, command=lambda:pressDP())
- button7_3.place(x=135, y=345, width=62.5, height=35)
- # ----等于
- button7_4 = tkinter.Button(text='=', bg='#708069', bd=2, command=lambda: pressOperator('='))
- button7_4.place(x=192.5, y=345, width=62.5, height=35)
- root.mainloop()
main.py
- from Init import *
- from Style import *
- if __name__ == '__main__':
- Demo()
温馨提示
若有求源码的需求,或复制时代码出现问题需要解答的人,可用QQ/Wechat:921899847联系。添加好友时,请备注:需代码支援。
推荐阅读