文章背景

本人已完成仿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_CALCif IS_CALC:CurrentShow.set('0')IS_CALC = Falseif CurrentShow.get() == '0':CurrentShow.set(number)else:if len(CurrentShow.get()) < MAXSHOWLEN:CurrentShow.set(CurrentShow.get() + number)#按下小数点#
def pressDP():global IS_CALCif IS_CALC:CurrentShow.set('0')IS_CALC = Falseif len(CurrentShow.get().split('.')) == 1:if len(CurrentShow.get()) < MAXSHOWLEN:CurrentShow.set(CurrentShow.get() + '.')#清零#
def clearAll():global STORAGEglobal IS_CALCSTORAGE.clear()IS_CALC = FalseCurrentShow.set('0')#清除当前显示框内所有数字#
def clearCurrent():CurrentShow.set('0')#删除显示框内最后一个数字#
def delOne():global IS_CALCif IS_CALC:CurrentShow.set('0')IS_CALC = Falseif 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 STORAGEglobal IS_CALCif 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 = Trueelif operator == 'sqrt':try:result = math.sqrt(float(CurrentShow.get()))except:result = 'illegal operation'result = modifyResult(result)CurrentShow.set(result)IS_CALC = Trueelif operator=='x²':result=int(math.pow(int(CurrentShow.get()),2))result = modifyResult(result)CurrentShow.set(result)IS_CALC = Trueelif 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 = Trueelif 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 = Trueelif 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 clearbutton1_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 readbutton1_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 savebutton1_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)# --第四行# ----7button4_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)# ----8button4_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)# ----9button4_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)# --第五行# ----4button5_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)# ----5button5_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)# ----6button5_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)# --第六行# ----1button6_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)# ----2button6_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)# ----3button6_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联系。添加好友时,请备注:需代码支援。

基于Python实现仿Windows标准计算器相关推荐

  1. C#——简单的计算器(仿Windows 10计算器)

    问题描述 运用WPF技术,模仿Windows 10系统中计算机器(Calculator)程序,开发一个类似程序. 问题分析 注: MS:记忆当前显示的数字(Memory Save) MC:清除记忆的数 ...

  2. C#——《C#语言程序设计》实验报告——Windows桌面编程——简单的计算器(仿Windows 10计算器)

    一.实验目的 熟悉使用WPF进行界面编程的基本过程: 掌握WPF布局.控件.事件的使用. 二.实验内容 运用WPF技术,模仿Windows 10系统中计算机器(Calculator)程序,开发一个类似 ...

  3. java简单计算器课程设计_java仿windows简易计算器课程设计 源码+报告

    [实例简介] java仿windows简易计算器课程设计 源码+报告 课直接运行. [实例截图] [核心代码] Java课设-简易计算器 └── Java课设-简易计算器 ├── Java课程设计.d ...

  4. 基于.net之仿Windows画板设计

    基于.net之仿Windows画板设计 队 长:周 洋 小组成员:周寅莹 袁晓旭 江春鹏 蒋彬含 朱振宇 屈生辉 万里骏 彭子航 指导老师:余敦辉 所在班级:湖北大学计算机科学与技术2016级 摘要: ...

  5. 基于python高仿探迹源码

    基于python实现探迹SCRM 最近几年市面上出现了很多大数据应用的产品,前面出现天眼查.企查查.企信宝等工商信息应用的saas产品,最近工商信息的应用由查询企业 转化为查客户了,所以又出现了探迹. ...

  6. 基于python实现仿探迹和天眼

    基于python爬虫技术实现探迹SCRM .天眼查.企查查,最近几年市面上出现了很多大数据应用的产品,前面出现天眼查.企查查.企信宝等工商信息应用的saas产品,最近工商信息的应用由查询企业 转化为查 ...

  7. html+css+js 基于逆波兰式 99%还原windows标准计算器

    0. 起步 原生计算器 还原计算器 可以看到相差不大,仿站是初级前端的必备技巧,但是也需要熟悉css基本布局 1.html 网页结构由上到下分为 : [title] [功能区func] [show表达 ...

  8. arcmap中添加python脚本_基于Python脚本的ArcMap字段计算器分类赋值

    因为出差等等缘由,又没能很好的坚持记录博客,今天回来了,继续记录所学吧.python ArcMap中提供了"字段计算器工具",实际上就是对Sql语句进行了可视化封装,造成了一个具备 ...

  9. java swing计算机_使用java swing仿window7标准计算器界面

    完整代码 ----- package com.lfd.view; import java.awt.BorderLayout; import java.awt.Color; import java.aw ...

  10. 基于MATLAB的仿windows画图板功能的实现

    1.仿真预览 2.部分核心代码 % --- Executes on selection change in popupmenu2. function popupmenu2_Callback(hObje ...

最新文章

  1. Ubuntu任务栏Tint2安装与使用
  2. 【高并发】你敢信??HashMap竟然干掉了CPU!!
  3. Spring Cloud Alibaba 新版本发布:众多期待内容整合打包加入!
  4. mxnet转onnx
  5. 一起学 c++(二)
  6. CentOS 分区方案
  7. ecology9 后端开发环境搭建_利用Vagrant快速搭建开发环境
  8. 【广告技术】如何提升定向广告效果?腾讯广告提出高质量负实例生成新方法
  9. [Java] 蓝桥杯ALGO-42 算法训练 送分啦
  10. 谷粒学院权限管理模块
  11. Activiti6--入门学习--基础知识环境搭建部署
  12. 前端加密JS库—CryptoJS
  13. 极光推送java demo_极光推送JAVA代码示例
  14. 小学计算机无生试讲教案,小学英语无生试讲
  15. kali最高权限root
  16. 京津冀地区地貌类型空间分布数据
  17. TextView的setBounds()方法
  18. ping ping ping HDU - 6203
  19. 浅谈傅里叶——5. 短时傅里叶的缺点与卷积的基本概念
  20. FastReport for Delphi

热门文章

  1. 《UNIX编程艺术》--读书笔记
  2. 优秀的jquery插件
  3. 计算机网络--基站 NFC 蓝牙 RFID ETC 云计算 云桌面
  4. 300字总结计算机flash,Flash学习心得体会范文
  5. SPSS教程-t检验怎么做?
  6. HTML+CSS静态页面网页设计作业:我的家乡网站设计——我的家乡-莆仙(6页)
  7. 招聘工作总结(精选多篇)
  8. iOS APP安全杂谈
  9. 久其报表大厅_久其报表是什么?
  10. eclipse保护眼睛色设置