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_())

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

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。