python实现计算器简易版


Posted in Python onDecember 17, 2020

Python计算器加减乘除,供大家参考,具体内容如下

1、效果图

python实现计算器简易版

2、代码

# coding=utf-8

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QLCDNumber, QSlider, QVBoxLayout, qApp, \
  QMainWindow
from PyQt5.QtCore import Qt


class ForExample(QWidget):

  def __init__(self):
    super().__init__()
    #
    self.clu = 0.0
    # print('lx',type(self.clu))
    self.info = 0.0
    print(type(self.info))
    self.statusSUN = False
    self.statusPoint = False
    self.initUI()

  def initUI(self):

    # 网格布局
    grid = QGridLayout()
    self.setLayout(grid)

    # names = ['清除', '后退', '', '关闭',
    #     '7', '8', '9', '/',
    #     '4', '5', '6', '*',
    #     '1', '2', '3', '-',
    #     '0', '.', '=', '+']

    # 创建一个菜单栏
    # menubar = self.menuBar()
    # # 添加菜单
    # fileMenu = menubar.addMenu('&File')

    # LCD显示屏
    self.lcd = QLCDNumber(self)
    self.lcd.setDigitCount(9)
    self.lcd.isRightToLeft()
    self.lcd.display(self.info)
    grid.addWidget(self.lcd, 0, 0, 2, 4)

    # 清除
    bc = QPushButton('清除')
    bc.clicked.connect(self.def_bc)
    grid.addWidget(bc, 2, 0)

    # 后退
    bk = QPushButton('后退')
    bk.clicked.connect(self.def_bk)
    grid.addWidget(bk, 2, 1)

    # ca = QPushButton('计算')
    # ca.clicked.connect(self.def_bc)
    # grid.addWidget(ca, 2, 2)

    of = QPushButton('关闭')
    of.clicked.connect(qApp.quit)
    grid.addWidget(of, 2, 3)
    #
    b7 = QPushButton('7')
    b7.clicked.connect(self.def_b7)
    grid.addWidget(b7, 3, 0)

    b8 = QPushButton('8')
    b8.clicked.connect(self.def_b8)
    grid.addWidget(b8, 3, 1)

    b9 = QPushButton('9')
    b9.clicked.connect(self.def_b9)
    grid.addWidget(b9, 3, 2)

    b4 = QPushButton('4')
    b4.clicked.connect(self.def_b4)
    grid.addWidget(b4, 4, 0)

    b5 = QPushButton('5')
    b5.clicked.connect(self.def_b5)
    grid.addWidget(b5, 4, 1)

    b6 = QPushButton('6')
    b6.clicked.connect(self.def_b6)
    grid.addWidget(b6, 4, 2)

    bExcept = QPushButton('/')
    bExcept.clicked.connect(self.def_bExcept)
    grid.addWidget(bExcept, 3, 3)

    bMultiply = QPushButton('*')
    bMultiply.clicked.connect(self.def_bMultiply)
    grid.addWidget(bMultiply, 4, 3)

    b1 = QPushButton('1')
    b1.clicked.connect(self.def_b1)
    grid.addWidget(b1, 5, 0)

    b2 = QPushButton('2')
    b2.clicked.connect(self.def_b2)
    grid.addWidget(b2, 5, 1)

    b3 = QPushButton('3')
    b3.clicked.connect(self.def_b3)
    grid.addWidget(b3, 5, 2)

    bLess = QPushButton('-')
    bLess.clicked.connect(self.def_bLess)
    grid.addWidget(bLess, 5, 3)

    b0 = QPushButton('0')
    b0.clicked.connect(self.def_b0)
    grid.addWidget(b0, 6, 0)

    bPoint = QPushButton('.')
    bPoint.clicked.connect(self.def_bPoint)
    grid.addWidget(bPoint, 6, 1)

    bsun = QPushButton('+')
    bsun.clicked.connect(self.def_bsun)
    grid.addWidget(bsun, 6, 3)

    bsun = QPushButton('=')
    bsun.clicked.connect(self.def_bresult)
    grid.addWidget(bsun, 6, 2)

    self.setGeometry(300, 300, 250, 250)
    self.show()

  #############################################################
  def def_bc(self):

    self.info = 0
    self.clu = 0
    self.statusSUN = False
    self.lcd.display(self.info)

  def def_bk(self):

    if len(str(self.info)) > 1:
      self.info = int(str(self.info)[:-1])
    else:
      self.info = 0

    self.lcd.display(self.info)

  def def_b7(self):

    if self.info == 0:
      self.info = 7
    else:
      self.info = int(str(self.info) + '7')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b8(self):

    if self.info == 0:
      self.info = 8
    else:
      self.info = int(str(self.info) + '8')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b9(self):

    if self.info == 0:
      self.info = 9
    else:
      self.info = int(str(self.info) + '9')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 除
  def def_bExcept(self):
    pass

  def def_b4(self):

    if self.info == 0:
      self.info = 4
    else:
      self.info = int(str(self.info) + '4')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b5(self):

    if self.info == 0:
      self.info = 5
    else:
      self.info = int(str(self.info) + '5')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b6(self):

    if self.info == 0:
      self.info = 6
    else:
      self.info = int(str(self.info) + '6')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 乘
  def def_bMultiply(self):
    pass

  def def_b1(self):

    if self.info == 0:
      self.info = 1
    else:
      self.info = int(str(self.info) + '1')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b2(self):

    if self.info == 0:
      self.info = 2
    else:
      self.info = int(str(self.info) + '2')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b3(self):

    if self.info == 0:
      self.info = 3
    else:
      self.info = int(str(self.info) + '3')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 减
  def def_bLess(self):
    pass

  def def_b0(self):

    if self.info == 0:
      self.info = 0
    else:
      self.info = int(str(self.info) + '0')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_bPoint(self):
    if self.statusPoint:
      pass
    else:
      self.statusPoint = True

    if self.info == 0:
      self.info = 0.
    else:
      self.info = int(str(self.info) + '.')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 等于,根据self.status判断输入的计算符号
  def def_bresult(self):

    self.clu += self.info
    self.lcd.display(self.clu)
    self.statusSUN = False
    self.info = 0

  # 加
  def def_bsun(self):

    self.clu += self.info
    self.lcd.display(self.clu)
    self.statusSUN = True
    self.info = 0


if __name__ == '__main__':
  app = QApplication(sys.argv)
  fe = ForExample()
  sys.exit(app.exec_()) # coding=utf-8

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QLCDNumber, QSlider, QVBoxLayout, qApp, \
  QMainWindow
from PyQt5.QtCore import Qt


class ForExample(QWidget):

  def __init__(self):
    super().__init__()
    #
    self.clu = 0.0
    # print('lx',type(self.clu))
    self.info = 0.0
    print(type(self.info))
    self.statusSUN = False
    self.statusPoint = False
    self.initUI()

  def initUI(self):

    # 网格布局
    grid = QGridLayout()
    self.setLayout(grid)

    # names = ['清除', '后退', '', '关闭',
    #     '7', '8', '9', '/',
    #     '4', '5', '6', '*',
    #     '1', '2', '3', '-',
    #     '0', '.', '=', '+']

    # 创建一个菜单栏
    # menubar = self.menuBar()
    # # 添加菜单
    # fileMenu = menubar.addMenu('&File')

    # LCD显示屏
    self.lcd = QLCDNumber(self)
    self.lcd.setDigitCount(9)
    self.lcd.isRightToLeft()
    self.lcd.display(self.info)
    grid.addWidget(self.lcd, 0, 0, 2, 4)

    # 清除
    bc = QPushButton('清除')
    bc.clicked.connect(self.def_bc)
    grid.addWidget(bc, 2, 0)

    # 后退
    bk = QPushButton('后退')
    bk.clicked.connect(self.def_bk)
    grid.addWidget(bk, 2, 1)

    # ca = QPushButton('计算')
    # ca.clicked.connect(self.def_bc)
    # grid.addWidget(ca, 2, 2)

    of = QPushButton('关闭')
    of.clicked.connect(qApp.quit)
    grid.addWidget(of, 2, 3)
    #
    b7 = QPushButton('7')
    b7.clicked.connect(self.def_b7)
    grid.addWidget(b7, 3, 0)

    b8 = QPushButton('8')
    b8.clicked.connect(self.def_b8)
    grid.addWidget(b8, 3, 1)

    b9 = QPushButton('9')
    b9.clicked.connect(self.def_b9)
    grid.addWidget(b9, 3, 2)

    b4 = QPushButton('4')
    b4.clicked.connect(self.def_b4)
    grid.addWidget(b4, 4, 0)

    b5 = QPushButton('5')
    b5.clicked.connect(self.def_b5)
    grid.addWidget(b5, 4, 1)

    b6 = QPushButton('6')
    b6.clicked.connect(self.def_b6)
    grid.addWidget(b6, 4, 2)

    bExcept = QPushButton('/')
    bExcept.clicked.connect(self.def_bExcept)
    grid.addWidget(bExcept, 3, 3)

    bMultiply = QPushButton('*')
    bMultiply.clicked.connect(self.def_bMultiply)
    grid.addWidget(bMultiply, 4, 3)

    b1 = QPushButton('1')
    b1.clicked.connect(self.def_b1)
    grid.addWidget(b1, 5, 0)

    b2 = QPushButton('2')
    b2.clicked.connect(self.def_b2)
    grid.addWidget(b2, 5, 1)

    b3 = QPushButton('3')
    b3.clicked.connect(self.def_b3)
    grid.addWidget(b3, 5, 2)

    bLess = QPushButton('-')
    bLess.clicked.connect(self.def_bLess)
    grid.addWidget(bLess, 5, 3)

    b0 = QPushButton('0')
    b0.clicked.connect(self.def_b0)
    grid.addWidget(b0, 6, 0)

    bPoint = QPushButton('.')
    bPoint.clicked.connect(self.def_bPoint)
    grid.addWidget(bPoint, 6, 1)

    bsun = QPushButton('+')
    bsun.clicked.connect(self.def_bsun)
    grid.addWidget(bsun, 6, 3)

    bsun = QPushButton('=')
    bsun.clicked.connect(self.def_bresult)
    grid.addWidget(bsun, 6, 2)

    self.setGeometry(300, 300, 250, 250)
    self.show()

  #############################################################
  def def_bc(self):

    self.info = 0
    self.clu = 0
    self.statusSUN = False
    self.lcd.display(self.info)

  def def_bk(self):

    if len(str(self.info)) > 1:
      self.info = int(str(self.info)[:-1])
    else:
      self.info = 0

    self.lcd.display(self.info)

  def def_b7(self):

    if self.info == 0:
      self.info = 7
    else:
      self.info = int(str(self.info) + '7')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b8(self):

    if self.info == 0:
      self.info = 8
    else:
      self.info = int(str(self.info) + '8')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b9(self):

    if self.info == 0:
      self.info = 9
    else:
      self.info = int(str(self.info) + '9')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 除
  def def_bExcept(self):
    pass

  def def_b4(self):

    if self.info == 0:
      self.info = 4
    else:
      self.info = int(str(self.info) + '4')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b5(self):

    if self.info == 0:
      self.info = 5
    else:
      self.info = int(str(self.info) + '5')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b6(self):

    if self.info == 0:
      self.info = 6
    else:
      self.info = int(str(self.info) + '6')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 乘
  def def_bMultiply(self):
    pass

  def def_b1(self):

    if self.info == 0:
      self.info = 1
    else:
      self.info = int(str(self.info) + '1')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b2(self):

    if self.info == 0:
      self.info = 2
    else:
      self.info = int(str(self.info) + '2')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_b3(self):

    if self.info == 0:
      self.info = 3
    else:
      self.info = int(str(self.info) + '3')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 减
  def def_bLess(self):
    pass

  def def_b0(self):

    if self.info == 0:
      self.info = 0
    else:
      self.info = int(str(self.info) + '0')

    self.statusSUN = False
    self.lcd.display(self.info)

  def def_bPoint(self):
    if self.statusPoint:
      pass
    else:
      self.statusPoint = True

    if self.info == 0:
      self.info = 0.
    else:
      self.info = int(str(self.info) + '.')

    self.statusSUN = False
    self.lcd.display(self.info)

  # 等于,根据self.status判断输入的计算符号
  def def_bresult(self):

    self.clu += self.info
    self.lcd.display(self.clu)
    self.statusSUN = False
    self.info = 0

  # 加
  def def_bsun(self):

    self.clu += self.info
    self.lcd.display(self.clu)
    self.statusSUN = True
    self.info = 0


if __name__ == '__main__':
  app = QApplication(sys.argv)
  fe = ForExample()
  sys.exit(app.exec_())

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python self,cls,decorator的理解
Jul 13 Python
跟老齐学Python之数据类型总结
Sep 24 Python
使用matplotlib画散点图的方法
May 25 Python
Django unittest 设置跳过某些case的方法
Dec 26 Python
对python指数、幂数拟合curve_fit详解
Dec 29 Python
在python中获取div的文本内容并和想定结果进行对比详解
Jan 02 Python
python3对拉勾数据进行可视化分析的方法详解
Apr 03 Python
python检测服务器端口代码实例
Aug 31 Python
Python文件路径名的操作方法
Oct 30 Python
使用PyTorch实现MNIST手写体识别代码
Jan 18 Python
关于python3.9安装wordcloud出错的问题及解决办法
Nov 02 Python
Python3自带工具2to3.py 转换 Python2.x 代码到Python3的操作
Mar 03 Python
利用Python实现自动扫雷小脚本
Dec 17 #Python
用python读取xlsx文件
Dec 17 #Python
Python实现自动整理文件的脚本
Dec 17 #Python
Python用access判断文件是否被占用的实例方法
Dec 17 #Python
Python实例教程之检索输出月份日历表
Dec 16 #Python
使用Python判断一个文件是否被占用的方法教程
Dec 16 #Python
python中类与对象之间的关系详解
Dec 16 #Python
You might like
十大“创意”战术!
2020/03/04 星际争霸
ThinkPHP连接数据库及主从数据库的设置教程
2014/08/22 PHP
php格式化日期实例分析
2014/11/12 PHP
PHP与服务器文件系统的简单交互
2016/10/21 PHP
PHP基于DOM创建xml文档的方法示例
2017/02/08 PHP
W3C Group的JavaScript1.8 新特性介绍
2009/05/19 Javascript
JavaScript高级程序设计 读书笔记之十一 内置对象Global
2012/03/07 Javascript
json数据处理技巧(字段带空格、增加字段、排序等等)
2013/06/14 Javascript
两个数组去重的JS代码
2013/12/04 Javascript
动态读取JSON解析键值对的方法
2014/06/03 Javascript
javascript学习笔记(六)数据类型和JSON格式
2014/10/08 Javascript
jquery让指定的元素闪烁显示的方法
2015/03/17 Javascript
jquery实现用户信息修改验证输入方法汇总
2015/07/18 Javascript
如何用jQuery实现ASP.NET GridView折叠伸展效果
2015/09/26 Javascript
实例详解jQuery表单验证插件validate
2016/01/18 Javascript
深入浅析AngularJS和DataModel
2016/02/16 Javascript
JavaScript入门教程之引用类型
2016/05/04 Javascript
JavaScript ES6中CLASS的使用详解
2016/11/22 Javascript
详解获取jq ul第一个li定位的四种解决方案
2016/11/23 Javascript
jQuery EasyUI 页面加载等待及页面等待层
2017/02/06 Javascript
JavaScript实现的鼠标响应颜色渐变效果完整实例
2017/02/18 Javascript
微信小程序商城项目之商品属性分类(4)
2017/04/17 Javascript
ES6 javascript的异步操作实例详解
2017/10/30 Javascript
微信小程序自定义toast弹窗效果的实现代码
2018/11/15 Javascript
微信小程序代码上传、审核发布小程序
2019/05/18 Javascript
用Python遍历C盘dll文件的方法
2015/05/06 Python
Python实现微信翻译机器人的方法
2019/08/13 Python
python双端队列原理、实现与使用方法分析
2019/11/27 Python
Python图片的横坐标汉字实例
2019/12/04 Python
Opencv图像处理:如何判断图片里某个颜色值占的比例
2020/06/03 Python
重新定义牛仔布,100美元以下:Warp + Weft
2018/07/25 全球购物
什么是静态路由?什么是动态路由?各自的特点是什么?
2015/09/16 面试题
十一国庆节“向国旗敬礼”主题班会活动方案
2014/09/27 职场文书
纪律委员竞选稿
2015/11/19 职场文书
解决hive中导入text文件遇到的坑
2021/04/07 Python
php将xml转化对象的实例详解
2021/11/17 PHP