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实现获取客户机上指定文件并传输到服务器的方法
Mar 16 Python
Python中的random()方法的使用介绍
May 15 Python
Python类的用法实例浅析
May 27 Python
python列表操作之extend和append的区别实例分析
Jul 28 Python
python中set常用操作汇总
Jun 30 Python
Python中内置的日志模块logging用法详解
Jul 12 Python
Python可变参数用法实例分析
Apr 02 Python
python如何实现int函数的方法示例
Feb 19 Python
详解用Python为直方图绘制拟合曲线的两种方法
Aug 21 Python
Python安装与卸载流程详细步骤(图解)
Feb 20 Python
python读取当前目录下的CSV文件数据
Mar 11 Python
python实现三种随机请求头方式
Jan 05 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
PHP 的几个配置文件函数
2006/12/21 PHP
[原创]效率较高的php下读取文本文件的代码
2008/07/02 PHP
php函数之子字符串替换 str_replace
2011/03/23 PHP
PHP开发框架kohana中处理ajax请求的例子
2014/07/14 PHP
php删除数组中重复元素的方法
2015/12/22 PHP
Symfony2函数用法实例分析
2016/03/18 PHP
Yii视图CGridView列表用法实例分析
2016/07/12 PHP
PHP使用Session实现上传进度功能详解
2019/08/06 PHP
[IE&FireFox兼容]JS对select操作
2007/01/07 Javascript
javascript不可用的问题探究
2013/10/01 Javascript
js页面跳转的常用方法整理
2013/10/18 Javascript
IE6下javasc#ipt:void(0) 无效的解决方法
2013/12/23 Javascript
JS实现清除指定cookies的方法
2014/09/20 Javascript
javascript中var的重要性分析
2015/02/11 Javascript
深入理解node.js之path模块
2017/05/03 Javascript
js下载文件并修改文件名
2017/05/08 Javascript
如何在AngularJs中调用第三方插件库
2017/05/21 Javascript
基于Vue开发数字输入框组件
2017/12/19 Javascript
Vue运用transition实现过渡动画
2019/05/06 Javascript
layui多图上传实现删除功能的例子
2019/09/23 Javascript
微信h5静默和非静默授权获取用户openId的方法和步骤
2020/06/08 Javascript
vue-cli3自动消除console.log()的调试信息方式
2020/10/21 Javascript
python中的字典使用分享
2016/07/31 Python
详解Python实现按任意键继续/退出的功能
2016/08/19 Python
Python入门之三角函数sin()函数实例详解
2017/11/08 Python
python安装scipy的方法步骤
2019/06/26 Python
python找出因数与质因数的方法
2019/07/25 Python
pytorch随机采样操作SubsetRandomSampler()
2020/07/07 Python
CSS3 clip-path 用法介绍详解
2018/03/01 HTML / CSS
北京某公司的.net笔试题
2014/03/20 面试题
js正则匹配markdown里的图片标签的实现
2021/03/24 Javascript
教学实习自我评价
2014/01/28 职场文书
中级会计职业生涯规划书
2014/03/01 职场文书
2015年计生协会工作总结
2015/04/24 职场文书
城南旧事观后感
2015/06/11 职场文书
logback如何自定义日志存储
2021/08/30 Java/Android