使用qt designer ,按装anaconda后,在如下路径找到:

conda3.05\Library\bin

designer.exe文件,双击启动:

创建窗体,命名为XiaoDing,整个的界面如下所示:

qt 设计器提供的常用控件基本都能满足开发需求,通过拖动左侧的控件,很便捷的就能搭建出如下的UI界面,比传统的手写控件代码要方便很多。

最终设计的计算器XiaoDing界面如下,

比如,其中一个用于计算器显示的对象:lcdNumber,对象的类型为:LCD Number。右侧为计算器中用到的所有对象。

2 转py文件

使用如下命令,将设计好的ui文件转为py文件

pyuic5 -o ./calculator/MainWindow.py ./calculator/mainwindow.ui

3 计算器实现逻辑

导入库:

  1. from PyQt5.QtGui import *
  2. from PyQt5.QtWidgets import *
  3. from PyQt5.QtCore import *
  4. import operator
  5. from MainWindow import Ui_MainWindow

主题代码逻辑很精简:

  1. # Calculator state.
  2. READY = 0
  3. INPUT = 1
  4. class MainWindow(QMainWindow, Ui_MainWindow):
  5.     def __init__(self, *args, **kwargs):
  6.         super(MainWindow, self).__init__(*args, **kwargs)
  7.         self.setupUi(self)
  8.         # Setup numbers.
  9.         for n in range(0, 10):
  10.             getattr(self, 'pushButton_n%s' % n).pressed.connect(lambda v=n: self.input_number(v))
  11.         # Setup operations.
  12.         self.pushButton_add.pressed.connect(lambda: self.operation(operator.add))
  13.         self.pushButton_sub.pressed.connect(lambda: self.operation(operator.sub))
  14.         self.pushButton_mul.pressed.connect(lambda: self.operation(operator.mul))
  15.         self.pushButton_div.pressed.connect(lambda: self.operation(operator.truediv))  # operator.div for Python2.7
  16.         self.pushButton_pc.pressed.connect(self.operation_pc)
  17.         self.pushButton_eq.pressed.connect(self.equals)
  18.         # Setup actions
  19.         self.actionReset.triggered.connect(self.reset)
  20.         self.pushButton_ac.pressed.connect(self.reset)
  21.         self.actionExit.triggered.connect(self.close)
  22.         self.pushButton_m.pressed.connect(self.memory_store)
  23.         self.pushButton_mr.pressed.connect(self.memory_recall)
  24.         self.memory = 0
  25.         self.reset()
  26.         self.show()

基础方法:

  1.     def input_number(self, v):
  2.         if self.state == READY:
  3.             self.state = INPUT
  4.             self.stack[-1] = v
  5.         else:
  6.             self.stack[-1] = self.stack[-1] * 10 + v
  7.         self.display()
  8.     def display(self):
  9.         self.lcdNumber.display(self.stack[-1])

按钮RE,M, RE对应的实现逻辑:

  1.     def reset(self):
  2.         self.state = READY
  3.         self.stack = [0]
  4.         self.last_operation = None
  5.         self.current_op = None
  6.         self.display()
  7.     def memory_store(self):
  8.         self.memory = self.lcdNumber.value()
  9.     def memory_recall(self):
  10.         self.state = INPUT
  11.         self.stack[-1] = self.memory
  12.         self.display()

+,-,x,/,/100对应实现方法:

  1. def operation(self, op):
  2.         if self.current_op:  # Complete the current operation
  3.             self.equals()
  4.         self.stack.append(0)
  5.         self.state = INPUT
  6.         self.current_op = op
  7.     def operation_pc(self):
  8.         self.state = INPUT
  9.         self.stack[-1] *= 0.01
  10.         self.display()

=号对应的方法实现:

  1.  def equals(self):
  2.         if self.state == READY and self.last_operation:
  3.             s, self.current_op = self.last_operation
  4.             self.stack.append(s)
  5.         if self.current_op:
  6.             self.last_operation = self.stack[-1], self.current_op
  7.             try:
  8.                 self.stack = [self.current_op(*self.stack)]
  9.             except Exception:
  10.                 self.lcdNumber.display('Err')
  11.                 self.stack = [0]
  12.             else:
  13.                 self.current_op = None
  14.                 self.state = READY
  15.                 self.display()

main函数:

  1. if __name__ == '__main__':
  2.     app = QApplication([])
  3.     app.setApplicationName("XiaoDing")
  4.     window = MainWindow()
  5.     app.exec_()

 

使用qt designer ,按装anaconda后,在如下路径找到:

conda3.05\Library\bin

designer.exe文件,双击启动:

创建窗体,命名为XiaoDing,整个的界面如下所示:

qt 设计器提供的常用控件基本都能满足开发需求,通过拖动左侧的控件,很便捷的就能搭建出如下的UI界面,比传统的手写控件代码要方便很多。

最终设计的计算器XiaoDing界面如下,

比如,其中一个用于计算器显示的对象:lcdNumber,对象的类型为:LCD Number。右侧为计算器中用到的所有对象。

2 转py文件

使用如下命令,将设计好的ui文件转为py文件

pyuic5 -o ./calculator/MainWindow.py ./calculator/mainwindow.ui

3 计算器实现逻辑

导入库:

  1. from PyQt5.QtGui import *from PyQt5.QtWidgets import *from PyQt5.QtCore import *
  2. import operator
  3. from MainWindow import Ui_MainWindow

主题代码逻辑很精简:

  1. INPUT = 
  2. , ):            getattr(self,         self.reset()
  3.         self.show()

基础方法:

  1.      + v
  2.         self.display()
  3.     def display(self):        self.lcdNumber.display(self.stack[-1])

按钮RE,M, RE对应的实现逻辑:

  1.     ]        self.last_operation = None        self.current_op = None        self.display()
  2.     def memory_store(self):        self.memory = self.lcdNumber.value()
  3.     def memory_recall(self):        self.state = INPUT        self.stack[-1] = self.memory        self.display()

+,-,x,/,/100对应实现方法:

  1. )        self.state = INPUT        self.current_op = op
  2.     def operation_pc(self):        self.state = INPUT        self.stack[-1] *= 0.01        self.display()

=号对应的方法实现:

  1.  ]            else:                self.current_op = None                self.state = READY                self.display()

main函数:

  1. if __name__ == '__main__':    app = QApplication([])    app.setApplicationName("XiaoDing")
  2.     window = MainWindow()    app.exec_()

Python定做一个计算器,小而美哒~的更多相关文章

  1. 如何用Python写一个计算器软件 附带效果图

    该计算器使用Python  tkinter模块开发 效果如下图 import tkinter #导入tkinter模块 root = tkinter.Tk() root.minsize(280,500 ...

  2. 用python编写一个计算器

    # 1 - 2 * ((60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2)))# 通过Pyt ...

  3. python造一个计算器

    正则表达式之简易计算器 关注公众号"轻松学编程"了解更多. 需求:使用正则表达式完成一个简易计算器. 功能:能够计算简单的表达式. 如:12((1+2)/(2+3)+1)*5.1- ...

  4. python 实现一个计算器功能

    #s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )' #第 ...

  5. 用python实现一个计算器

    import re def atom_cal(exp): # 计算乘除法 if '*' in exp: a,b = exp.split('*') return str(float(a) * float ...

  6. 利用Python代码编写计算器小程序

    import tkinter import tkinter.messagebox import math class JSQ: def __init__(self): #创建主界面 self.root ...

  7. 用Python开发实用程序 – 计算器

    一段时间前,自己制作了一个库 “sui-math”.这其实是math的翻版.做完后,python既然可以轻易的完成任何的数学计算,何不用python开发一个小程序专门用以计算呢? 现在我们越来越依赖于 ...

  8. 20204107 孙嘉临《PYTHON程序设计》计算器设计实验二报告

    课程:<python程序设计> 班级:2041 姓名:孙嘉临 学号:20204107 实验教师:王志强 实验日期:2021年4月12日 必修/选修:公选课 ##一.实验内容 设计并完成一个 ...

  9. 用python做个计算器不是轻轻松松吗~

    计算器 Kivy是一个免费的开源Python库,可以快速轻松地开发高度交互的跨平台应用程序. 这里我将使用Python中的Kivy包来构建一个计算器GUI.(https://jq.qq.com/?_w ...

随机推荐

  1. mysql插入的时间莫名的加一秒

    1.问题描述 我获取当天最大的时间: public static Date getEndOfDay(Date date) { LocalDateTime localDateTime = LocalDa ...

  2. 使用jieba分析小说太古神王中,男主更爱谁?去文章中找答案吧!#华为云·寻找黑马程序员#

    欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...

  3. Python--glob模块

    0.glob模块和通配符 glob模块最主要的方法有2个: 1.glob() 2.iglob() 以上2分方法一般和通配符一起使用,常用的通配符有3个: * :匹配零个或多个字符 ? :匹配任何单个的 ...

  4. openlayers4 入门开发系列结合 echarts4 实现统计图(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  5. hdu4585Shaolin

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题意: 第一个人ID为1,战斗力为1e9. 给定n,给出n个人的ID和战斗力. 每个人必须和战斗 ...

  6. ThreadLocal的进化——TransmittableThreadLocal

    上一篇文章中,我们谈到了 InheritableThreadLocal,它解决了 ThreadLocal 针对父子线程无法共享上下文的问题.但我们可能听说过阿里的开源产品TransmittableTh ...

  7. Kerberos+SSH安装配置使用教程

    一.背景说明 最早听说KDC和Kerberos应该是大三的<应用密码学>,当时感觉这套对称密钥分发机制比非对称密钥的PKI分发机制要好理解.但几年下来由于现实中使用SSL的场景比较比(主要 ...

  8. Linux-tac、diff、tree、echo、seq、重定向

    1.tac  方向输出文件,最后一行放在第一行的位置输出 2.diff  比较文件的内容 vimdiff:在vim中比较 3. tree  树状图显示目录内容 -d 只显示目录   -L  树状 目录 ...

  9. centos与内核版本对应关系

    centos是基于redhat的二次开发,redhat会封装不同版本的内核,有时候,我们需要指定内核版本的centos,下面两个网站或许对你有帮助: https://access.redhat.com ...

  10. Python3 函数进阶3

    目录 匿名函数 定义匿名函数 匿名函数的使用 内置函数 匿名函数 定义匿名函数 我们之前定义的函数都是有名函数, 我们可以通过函数名来调用 匿名函数顾名思义就是一种没有绑定函数名的函数, 使用一次既被 ...