PyQt5实现简易计算器


Posted in Python onMay 30, 2020

本文实例为大家分享了PyQt5实现简易计算器的具体代码,供大家参考,具体内容如下

效果图:

PyQt5实现简易计算器

界面代码

calc_interface.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'calc_interface.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
 MainWindow.setObjectName("MainWindow")
 MainWindow.resize(647, 675)
 self.centralwidget = QtWidgets.QWidget(MainWindow)
 self.centralwidget.setObjectName("centralwidget")
 self.b_1 = QtWidgets.QPushButton(self.centralwidget)
 self.b_1.setGeometry(QtCore.QRect(10, 330, 71, 71))
 self.b_1.setObjectName("b_1")
 self.b_2 = QtWidgets.QPushButton(self.centralwidget)
 self.b_2.setGeometry(QtCore.QRect(80, 330, 71, 71))
 self.b_2.setObjectName("b_2")
 self.b_3 = QtWidgets.QPushButton(self.centralwidget)
 self.b_3.setGeometry(QtCore.QRect(150, 330, 71, 71))
 self.b_3.setObjectName("b_3")
 self.b_6 = QtWidgets.QPushButton(self.centralwidget)
 self.b_6.setGeometry(QtCore.QRect(150, 400, 71, 71))
 self.b_6.setObjectName("b_6")
 self.b_4 = QtWidgets.QPushButton(self.centralwidget)
 self.b_4.setGeometry(QtCore.QRect(10, 400, 71, 71))
 self.b_4.setObjectName("b_4")
 self.b_5 = QtWidgets.QPushButton(self.centralwidget)
 self.b_5.setGeometry(QtCore.QRect(80, 400, 71, 71))
 self.b_5.setObjectName("b_5")
 self.b_8 = QtWidgets.QPushButton(self.centralwidget)
 self.b_8.setGeometry(QtCore.QRect(80, 470, 71, 71))
 self.b_8.setObjectName("b_8")
 self.b_9 = QtWidgets.QPushButton(self.centralwidget)
 self.b_9.setGeometry(QtCore.QRect(150, 470, 71, 71))
 self.b_9.setObjectName("b_9")
 self.b_7 = QtWidgets.QPushButton(self.centralwidget)
 self.b_7.setGeometry(QtCore.QRect(10, 470, 71, 71))
 self.b_7.setObjectName("b_7")
 self.b_eq = QtWidgets.QPushButton(self.centralwidget)
 self.b_eq.setGeometry(QtCore.QRect(150, 540, 211, 71))
 self.b_eq.setObjectName("b_eq")
 self.b_mod = QtWidgets.QPushButton(self.centralwidget)
 self.b_mod.setGeometry(QtCore.QRect(220, 400, 71, 71))
 self.b_mod.setObjectName("b_mod")
 self.b_bra_l = QtWidgets.QPushButton(self.centralwidget)
 self.b_bra_l.setGeometry(QtCore.QRect(220, 330, 71, 71))
 self.b_bra_l.setObjectName("b_bra_l")
 self.b_div = QtWidgets.QPushButton(self.centralwidget)
 self.b_div.setGeometry(QtCore.QRect(220, 260, 71, 71))
 self.b_div.setObjectName("b_div")
 self.b_add = QtWidgets.QPushButton(self.centralwidget)
 self.b_add.setGeometry(QtCore.QRect(10, 260, 71, 71))
 self.b_add.setObjectName("b_add")
 self.b_sub = QtWidgets.QPushButton(self.centralwidget)
 self.b_sub.setGeometry(QtCore.QRect(80, 260, 71, 71))
 self.b_sub.setObjectName("b_sub")
 self.b_mul = QtWidgets.QPushButton(self.centralwidget)
 self.b_mul.setGeometry(QtCore.QRect(150, 260, 71, 71))
 self.b_mul.setObjectName("b_mul")
 self.b_pow = QtWidgets.QPushButton(self.centralwidget)
 self.b_pow.setGeometry(QtCore.QRect(290, 260, 71, 71))
 self.b_pow.setObjectName("b_pow")
 self.b_pai = QtWidgets.QPushButton(self.centralwidget)
 self.b_pai.setGeometry(QtCore.QRect(290, 400, 71, 71))
 self.b_pai.setObjectName("b_pai")
 self.b_bra_r = QtWidgets.QPushButton(self.centralwidget)
 self.b_bra_r.setGeometry(QtCore.QRect(290, 330, 71, 71))
 self.b_bra_r.setObjectName("b_bra_r")
 self.l_hist = QtWidgets.QListWidget(self.centralwidget)
 self.l_hist.setGeometry(QtCore.QRect(380, 10, 256, 601))
 self.l_hist.setObjectName("l_hist")
 self.e_view = QtWidgets.QTextEdit(self.centralwidget)
 self.e_view.setGeometry(QtCore.QRect(10, 10, 351, 231))
 font = QtGui.QFont()
 font.setFamily("Adobe Caslon Pro Bold")
 font.setPointSize(22)
 font.setBold(True)
 font.setWeight(75)
 self.e_view.setFont(font)
 self.e_view.setObjectName("e_view")
 self.b_0 = QtWidgets.QPushButton(self.centralwidget)
 self.b_0.setGeometry(QtCore.QRect(80, 540, 71, 71))
 self.b_0.setObjectName("b_0")
 self.b_pt = QtWidgets.QPushButton(self.centralwidget)
 self.b_pt.setGeometry(QtCore.QRect(10, 540, 71, 71))
 self.b_pt.setObjectName("b_pt")
 self.b_del = QtWidgets.QPushButton(self.centralwidget)
 self.b_del.setGeometry(QtCore.QRect(220, 470, 71, 71))
 self.b_del.setObjectName("b_del")
 self.b_clc = QtWidgets.QPushButton(self.centralwidget)
 self.b_clc.setGeometry(QtCore.QRect(290, 470, 71, 71))
 self.b_clc.setObjectName("b_clc")
 MainWindow.setCentralWidget(self.centralwidget)
 self.menubar = QtWidgets.QMenuBar(MainWindow)
 self.menubar.setGeometry(QtCore.QRect(0, 0, 647, 30))
 self.menubar.setObjectName("menubar")
 MainWindow.setMenuBar(self.menubar)
 self.statusbar = QtWidgets.QStatusBar(MainWindow)
 self.statusbar.setObjectName("statusbar")
 MainWindow.setStatusBar(self.statusbar)

 self.retranslateUi(MainWindow)
 QtCore.QMetaObject.connectSlotsByName(MainWindow)

 def retranslateUi(self, MainWindow):
 _translate = QtCore.QCoreApplication.translate
 MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
 self.b_1.setText(_translate("MainWindow", "1"))
 self.b_2.setText(_translate("MainWindow", "2"))
 self.b_3.setText(_translate("MainWindow", "3"))
 self.b_6.setText(_translate("MainWindow", "6"))
 self.b_4.setText(_translate("MainWindow", "4"))
 self.b_5.setText(_translate("MainWindow", "5"))
 self.b_8.setText(_translate("MainWindow", "8"))
 self.b_9.setText(_translate("MainWindow", "9"))
 self.b_7.setText(_translate("MainWindow", "7"))
 self.b_eq.setText(_translate("MainWindow", "="))
 self.b_mod.setText(_translate("MainWindow", "%"))
 self.b_bra_l.setText(_translate("MainWindow", "("))
 self.b_div.setText(_translate("MainWindow", "/"))
 self.b_add.setText(_translate("MainWindow", "+"))
 self.b_sub.setText(_translate("MainWindow", "-"))
 self.b_mul.setText(_translate("MainWindow", "*"))
 self.b_pow.setText(_translate("MainWindow", "^"))
 self.b_pai.setText(_translate("MainWindow", "π"))
 self.b_bra_r.setText(_translate("MainWindow", ")"))
 self.b_0.setText(_translate("MainWindow", "0"))
 self.b_pt.setText(_translate("MainWindow", "."))
 self.b_del.setText(_translate("MainWindow", "删除"))
 self.b_clc.setText(_translate("MainWindow", "清空"))

主逻辑代码

calc_main.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from calc_interface import Ui_MainWindow
import os,sys

global e_view

pluginsPath='PyQt5/Qt/plugins'
if os.path.exists(pluginsPath):#指定插件路径。源码运行时不会生效,打包后运行检测到路径,加载插件
 QApplication.addLibraryPath(pluginsPath)

class MyMainWindow(QMainWindow, Ui_MainWindow):

 def forge_link(self):
 self.b_0.clicked.connect(self.button_event(0))
 self.b_1.clicked.connect(self.button_event(1))
 self.b_2.clicked.connect(self.button_event(2))
 self.b_3.clicked.connect(self.button_event(3))
 self.b_4.clicked.connect(self.button_event(4))
 self.b_5.clicked.connect(self.button_event(5))
 self.b_6.clicked.connect(self.button_event(6))
 self.b_7.clicked.connect(self.button_event(7))
 self.b_8.clicked.connect(self.button_event(8))
 self.b_9.clicked.connect(self.button_event(9))
 self.b_add.clicked.connect(self.button_event('+'))
 self.b_sub.clicked.connect(self.button_event('-'))
 self.b_mul.clicked.connect(self.button_event('*'))
 self.b_div.clicked.connect(self.button_event('/'))
 self.b_pow.clicked.connect(self.button_event('**'))
 self.b_bra_l.clicked.connect(self.button_event('('))
 self.b_bra_r.clicked.connect(self.button_event(')'))
 self.b_mod.clicked.connect(self.button_event('%'))
 self.b_pai.clicked.connect(self.button_event('3.1415926'))
 self.b_pt.clicked.connect(self.button_event('.'))
 self.b_del.clicked.connect(self.delete_event)
 self.b_clc.clicked.connect(self.clear_event)
 self.b_eq.clicked.connect(self.calc_complish)

 def __init__(self, parent=None):
 super(MyMainWindow, self).__init__(parent)
 self.setupUi(self)
 self.forge_link() #连接槽函数

 def button_event(self,arg):
 # print(dir(self.e_view))
 global e_view
 e_view=self.e_view
 def fun(): #返回一个自定义的槽函数
  global e_view
  txt = e_view.toPlainText()
  e_view.setText(txt + str(arg))
 return fun

 def calc_complish(self):
 txt=self.e_view.toPlainText()
 ans=''
 try:
  ans=str(eval(txt))
 except BaseException:
  ans='MathError'
 # print(ans)
 self.clear_event()
 self.e_view.setText(ans)
 self.l_hist.addItem(txt+'='+ans)

 def clear_event(self):
 self.e_view.setText('')

 def delete_event(self):
 txt = self.e_view.toPlainText()
 txt=txt[:len(txt)-1]
 self.e_view.setText(txt)

if __name__ == '__main__':
 app=QApplication(sys.argv)
 myWin=MyMainWindow()
 myWin.show()
 sys.exit(app.exec())

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

Python 相关文章推荐
Python实现删除Android工程中的冗余字符串
Jan 19 Python
python中list常用操作实例详解
Jun 03 Python
Python实现图像几何变换
Jul 06 Python
动感网页相册 python编写简单文件夹内图片浏览工具
Aug 17 Python
python win32 简单操作方法
May 25 Python
定制FileField中的上传文件名称实例
Aug 23 Python
基于tensorflow加载部分层的方法
Jul 26 Python
Python实现的IP端口扫描工具类示例
Feb 15 Python
详解python selenium 爬取网易云音乐歌单名
Mar 28 Python
PYTHON实现SIGN签名的过程解析
Oct 28 Python
PyTorch实现更新部分网络,其他不更新
Dec 31 Python
VSCode基础使用与VSCode调试python程序入门的图文教程
Mar 30 Python
Python实现简单层次聚类算法以及可视化
Mar 18 #Python
PyQt5实现简单数据标注工具
Mar 18 #Python
Python简单I/O操作示例
Mar 18 #Python
python实现简单图片物体标注工具
Mar 18 #Python
Python面向对象程序设计之类的定义与继承简单示例
Mar 18 #Python
Python动态赋值的陷阱知识点总结
Mar 17 #Python
Python将字符串常量转化为变量方法总结
Mar 17 #Python
You might like
PHP中实现中文字符进制转换原理分析
2011/12/06 PHP
ThinkPHP类似AOP思想的参数验证的实现方法
2019/12/18 PHP
用javascript控制iframe滚动的代码
2007/04/10 Javascript
kmock javascript 单元测试代码
2011/02/06 Javascript
namespace.js Javascript的命名空间库
2011/10/11 Javascript
js随机生成网页背景颜色的方法
2015/02/26 Javascript
jQuery插件Elastislide实现响应式的焦点图无缝滚动切换特效
2015/04/12 Javascript
jquery实现简单合拢与展开网页面板的方法
2015/09/01 Javascript
jquery插件jquery.LightBox.js实现点击放大图片并左右点击切换效果(附demo源码下载)
2016/02/25 Javascript
JavaScript实现经典排序算法之选择排序
2016/12/28 Javascript
Angularjs在360兼容模式下取数据缓存问题的解决办法
2017/06/22 Javascript
Vue.js仿微信聊天窗口展示组件功能
2017/08/11 Javascript
web前端vue之vuex单独一文件使用方式实例详解
2018/01/11 Javascript
AngularJS监听ng-repeat渲染完成的两种方法
2018/01/16 Javascript
react router4+redux实现路由权限控制的方法
2018/05/03 Javascript
Vue中的异步组件函数实现代码
2018/07/20 Javascript
微信 jssdk 签名错误invalid signature的解决方法
2019/01/14 Javascript
使用weixin-java-miniapp配置进行单个小程序的配置详解
2019/03/29 Javascript
JavaScript一元正号运算符示例代码
2019/06/30 Javascript
微信小程序 调用微信授权窗口相关问题解决
2019/07/25 Javascript
详解小程序BackgroundAudioManager踩坑之旅
2019/12/08 Javascript
JS数组方法join()用法实例分析
2020/01/18 Javascript
Vue项目如何引入bootstrap、elementUI、echarts
2020/11/26 Vue.js
Python实现微信公众平台自定义菜单实例
2015/03/20 Python
python中aioysql(异步操作MySQL)的方法
2019/04/11 Python
利用Python的sympy包求解一元三次方程示例
2019/11/22 Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
2020/03/03 Python
python打包多类型文件的操作方法
2020/09/21 Python
全球领先的鞋类零售商:The Walking Company
2016/07/21 全球购物
英国街头品牌:Bee Inspired Clothing
2018/02/12 全球购物
教师岗位职责范本
2013/12/29 职场文书
大学新闻系自荐书
2014/05/31 职场文书
劳动者解除劳动合同通知书
2015/04/16 职场文书
2015年网管个人工作总结
2015/05/22 职场文书
PostgreSQL聚合函数介绍以及分组和排序
2022/04/12 PostgreSQL
pytorch实现加载保存查看checkpoint文件
2022/07/15 Python