使用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 计算器实现逻辑

导入库:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

import operator

from MainWindow import Ui_MainWindow

主题代码逻辑很精简:

# Calculator state.
READY = 0
INPUT = 1

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setupUi(self)

        # Setup numbers.
        for n in range(0, 10):
            getattr(self, 'pushButton_n%s' % n).pressed.connect(lambda v=n: self.input_number(v))

        # Setup operations.
        self.pushButton_add.pressed.connect(lambda: self.operation(operator.add))
        self.pushButton_sub.pressed.connect(lambda: self.operation(operator.sub))
        self.pushButton_mul.pressed.connect(lambda: self.operation(operator.mul))
        self.pushButton_div.pressed.connect(lambda: self.operation(operator.truediv))  # operator.div for Python2.7

        self.pushButton_pc.pressed.connect(self.operation_pc)
        self.pushButton_eq.pressed.connect(self.equals)

        # Setup actions
        self.actionReset.triggered.connect(self.reset)
        self.pushButton_ac.pressed.connect(self.reset)

        self.actionExit.triggered.connect(self.close)

        self.pushButton_m.pressed.connect(self.memory_store)
        self.pushButton_mr.pressed.connect(self.memory_recall)

        self.memory = 0
        self.reset()

        self.show()

基础方法:

    def input_number(self, v):
        if self.state == READY:
            self.state = INPUT
            self.stack[-1] = v
        else:
            self.stack[-1] = self.stack[-1] * 10 + v

        self.display()

    def display(self):
        self.lcdNumber.display(self.stack[-1])

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

    def reset(self):
        self.state = READY
        self.stack = [0]
        self.last_operation = None
        self.current_op = None
        self.display()

    def memory_store(self):
        self.memory = self.lcdNumber.value()

    def memory_recall(self):
        self.state = INPUT
        self.stack[-1] = self.memory
        self.display()

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

def operation(self, op):
        if self.current_op:  # Complete the current operation
            self.equals()

        self.stack.append(0)
        self.state = INPUT
        self.current_op = op

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

=号对应的方法实现:

 def equals(self):
        if self.state == READY and self.last_operation:
            s, self.current_op = self.last_operation
            self.stack.append(s)

        if self.current_op:
            self.last_operation = self.stack[-1], self.current_op

            try:
                self.stack = [self.current_op(*self.stack)]
            except Exception:
                self.lcdNumber.display('Err')
                self.stack = [0]
            else:
                self.current_op = None
                self.state = READY
                self.display()

main函数:

if __name__ == '__main__':
    app = QApplication([])
    app.setApplicationName("XiaoDing")

    window = MainWindow()
    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 计算器实现逻辑

导入库:

from PyQt5.QtGui import *from PyQt5.QtWidgets import *from PyQt5.QtCore import *

import operator

from MainWindow import Ui_MainWindow

主题代码逻辑很精简:

INPUT = 

, ):            getattr(self,         self.reset()

        self.show()

基础方法:

     + v

        self.display()

    def display(self):        self.lcdNumber.display(self.stack[-1])

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

    ]        self.last_operation = None        self.current_op = None        self.display()

    def memory_store(self):        self.memory = self.lcdNumber.value()

    def memory_recall(self):        self.state = INPUT        self.stack[-1] = self.memory        self.display()

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

)        self.state = INPUT        self.current_op = op

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

=号对应的方法实现:

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

main函数:

if __name__ == '__main__':    app = QApplication([])    app.setApplicationName("XiaoDing")

    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. 【Android - 自定义View】之自定义可滚动的流式布局

    首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 FlowLayout ,继承自ViewGroup类: (2)在这个自定义View中,用户可以放入所有继承自View类的视图,这个 ...

  2. idea中自定义快捷键

    idea中自定义快捷键 在开发的过程中,总要写一些重复的代码,那么使用自定义快捷键,可以将这些经常用到的代码,自动生成. 1.在idea工具中点击File-->Settings 2.在弹出来的界 ...

  3. iOS 自定义TabBarController

    转自:http://blog.csdn.net/xn4545945/article/details/35994863 一.自定义的思路 iOS中的TabBarController确实已经很强大了,大部 ...

  4. 使用java理解程序逻辑 试题分析

      1.编译Java Applet源程序文件产生的字节码文件的扩展名为() A:.java B..class C:Html D:Exe 正确答案:B 试题分析: 本题考查的是Java程序的开发过程.J ...

  5. 2018HDU多校训练-3-Problem M. Walking Plan

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6331 Walking Plan  Problem Description There are n inte ...

  6. git 设置和取消指定域名代理 - git config proxy

    Firstly - Check Check if U have global .gitconfig file 检查是否有全局 .gitconfig 文件 Usually global .gitconf ...

  7. 常见的linux快捷方式和英文错误提示

    第5章 linux常见的快捷方式 Ctrl +l 清屏的意思 2 Ctrl +c 退出当前的进程 3 Ctrl +w 删除光标到空格之间的信息 4 Ctrl +a 快速移动到光标行首 5 Ctrl + ...

  8. ARTS-S python抽象方法抽象类

    # coding: utf-8 from abc import ABC, abstractmethod class AbstractClassExample(ABC): def __init__(se ...

  9. 【JS】310- 使用 focusout 事件,解决 iOS 键盘收起不归位问题

    点击上方"前端自习课"关注,学习起来~ ,0);            }        }, 这时,我们问题得到解决了,当从输入框输入内容,然后点击键盘的完成收起键盘,效果符合我 ...

  10. 使用FileReader在浏览器读取预览文件(image和txt)

    如标题,之前在某个地方看到因为有Blob的存在,理论上可以在浏览器上查看所有格式的文件.自己想着试试现在暂时只能够查看图片和预览txt文件.其他的比如doc,docx格式的文件查看的时候是乱码 如图: ...