python实现计算器简易版

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

1、效果图

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

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

(0)

相关推荐

  • 利用Tkinter(python3.6)实现一个简单计算器

    前言 上机实践课程开始了,嗯,老师来了之后念了下PPT,然后说:开始做吧......... 然后就开始了Python的GUI之路,以前没接触过PYthon的可视化界面(虽然这样很不明智) 但是现在做起来感觉写小工具还挺方便的,当时搜到的第一个库便是Tkinter就直接开始写了 后来发现QT很不错的样子,下个实验就用QT吧.然后关于Tkinter(python3.6) 计算器源码 ennn.....有的命名不规范....... 首先对于python中栈的实现是通过list的方式模拟 pop()出栈

  • Python只用40行代码编写的计算器实例

    本文实例讲述了Python只用40行代码编写的计算器.分享给大家供大家参考,具体如下: 效果图: 代码: from tkinter import * reset=True def buttonCallBack(event): global label global reset num=event.widget['text'] if num=='C': label['text']="0" return if num in "=": label['text']=str(

  • Python PyQt5实现的简易计算器功能示例

    本文实例讲述了Python PyQt5实现的简易计算器功能.分享给大家供大家参考,具体如下: 这里剩下计算函数(self.calculator)未实现,有兴趣的朋友可以实现它 [知识点] 1.利用循环添加按钮部件,及给每个按钮设置信号/槽 2.给按钮设置固定大小:button.setFixedSize(QtCore.QSize(60,30)) 3.取事件的的发送者(此例为各个按钮)的文本: self.sender().text() [效果图] [源代码] import sys from PyQt

  • Python设计实现的计算器功能完整实例

    本文实例讲述了Python设计实现的计算器功能.分享给大家供大家参考,具体如下: 通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3)/(16-3*2)) 我的处理计算基本思路是: 解题思路是,需要优先处理内层括号运算--外层括号运算--先乘除后加减的原则: 1.正则处理用户输入的字符串,然后对其进行判断,判断计算公式是否有括号,有就先将计算公式进

  • python实现简易版计算器

    学了一周的Python,这篇文章算是为这段时间自学做的小总结. 一.Python简介 Python是一门十分优美的脚本语言,如果学过java.c++那入门Python是非常简单的.Python具有丰富和强大的类库.它常被昵称为胶水语言,它能够很轻松的把用其他语言制作的各种模块(尤其是C/C++)轻松地联结在一起.常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写. 二.Python版计算器的实现 工具准备: 1.

  • 仅用50行代码实现一个Python编写的计算器的教程

     简介 在这篇文章中,我将向大家演示怎样向一个通用计算器一样解析并计算一个四则运算表达式.当我们结束的时候,我们将得到一个可以处理诸如 1+2*-(-3+2)/5.6+3样式的表达式的计算器了.当然,你也可以将它拓展的更为强大. 我本意是想提供一个简单有趣的课程来讲解 语法分析 和 正规语法(编译原理内容).同时,介绍一下PlyPlus,这是一个我断断续续改进了好几年的语法解析 接口.作为这个课程的附加产物,我们最后会得到完全可替代eval()的一个安全的四则运算器. 如果你想在自家的电脑上试试

  • 基于python的Tkinter实现一个简易计算器

    本文实例介绍了基于python的Tkinter实现简易计算器的详细代码,分享给大家供大家参考,具体内容如下 第一种:使用python 的 Tkinter实现一个简易计算器 #coding:utf-8 from Tkinter import * import time root = Tk() def cacl(input_str): if "x" in input_str: ret = input_str.split("x") return int(ret[0]) *

  • Python实现的简单计算器功能详解

    本文实例讲述了Python实现的简单计算器功能.分享给大家供大家参考,具体如下: 使用python编写一款简易的计算器 计算器效果图 首先搭建计算器的面板: 计算器面板结构 建造一个继承于wx.Frame的frame,在init属性函数中搭建面板 class CalcFrame(wx.Frame):#建造一个继承于wx.Frame的frame def __init__(self,title): wx.Frame.__init__ (self,None,-1,title, pos=(100,300

  • python 简易计算器程序,代码就几行

    代码: 复制代码 代码如下: import os while True: dynamic = input('输入计算表达式:') if dynamic != 'cls': try: result = eval(dynamic) print('计算结果:'+str(result)) except: print('计算表达式输入有误!') else: command = 'cls' os.system(command)

  • Python实现简单的四则运算计算器

    一.算法 1.算法的主要思想就是将一个中缀表达式(Infix expression)转换成便于处理的后缀表达式(Postfix expression),然后借助于栈这个简单的数据结构,计算出表达式的结果. 2.关于如何讲普通的表达式转换成后缀表达式,以及如何处理后缀表达式并计算出结果的具体算法描述不在此叙述了,书上有详细的说明. 二.简易计算器 使用说明 使用该计算器类的简单示例如下: # usage c = Calculator() print('result: {:f}'.formart(c

随机推荐