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模块学习 datetime介绍
Aug 27 Python
Django中处理出错页面的方法
Jul 15 Python
Python的Django框架中模板碎片缓存简介
Jul 24 Python
玩转python爬虫之cookie使用方法
Feb 17 Python
python使用paramiko实现远程拷贝文件的方法
Apr 18 Python
python实现爬取百度图片的方法示例
Jul 06 Python
python数据爬下来保存的位置
Feb 17 Python
解决python执行较大excel文件openpyxl慢问题
May 15 Python
给keras层命名,并提取中间层输出值,保存到文档的实例
May 23 Python
python 识别登录验证码图片功能的实现代码(完整代码)
Jul 03 Python
python中turtle库的简单使用教程
Nov 11 Python
Selenium 安装和简单使用的实现
Dec 04 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 页面执行时间计算代码
2008/12/04 PHP
php地址引用(php地址引用的效率问题)
2012/03/23 PHP
php+ajax实现无刷新数据分页的办法
2015/11/02 PHP
js从10种颜色中随机取色实现每次取出不同的颜色
2013/10/23 Javascript
Hallo.js基于jQuery UI所见即所得的Web编辑器
2016/01/26 Javascript
每日十条JavaScript经验技巧(一)
2016/06/23 Javascript
微信小程序 WXML、WXSS 和JS介绍及详解
2016/10/08 Javascript
微信小程序开发图片拖拽实例详解
2017/05/05 Javascript
vue2.0中vue-cli实现全选、单选计算总价格的实例代码
2017/07/18 Javascript
webpack配置sass模块的加载的方法
2017/07/30 Javascript
浅谈vue项目4rs vue-router上线后history模式遇到的坑
2018/09/27 Javascript
Vue2.x中利用@font-size引入字体图标报错的解决方法
2018/09/28 Javascript
javascript实现摄像头拍照预览
2019/09/30 Javascript
node.js中path路径模块的使用方法实例分析
2020/02/13 Javascript
[01:08:09]DOTA2上海特级锦标赛主赛事日 - 1 胜者组第一轮#1Liquid VS Alliance第二局
2016/03/02 DOTA
python解析xml文件实例分享
2013/12/04 Python
python网页请求urllib2模块简单封装代码
2014/02/07 Python
由Python运算π的值深入Python中科学计算的实现
2015/04/17 Python
Python合并字符串的3种方法
2015/05/21 Python
python 转换 Javascript %u 字符串为python unicode的代码
2016/09/06 Python
python解决汉字编码问题:Unicode Decode Error
2017/01/19 Python
Python装饰器实现几类验证功能做法实例
2017/05/18 Python
利用Pandas和Numpy按时间戳将数据以Groupby方式分组
2019/07/22 Python
浅析python中while循环和for循环
2019/11/19 Python
python 实现让字典的value 成为列表
2019/12/16 Python
阿联酋团购网站:Groupon阿联酋
2016/10/14 全球购物
数控个人求职信范文
2014/02/03 职场文书
白酒市场营销方案
2014/02/25 职场文书
乡镇信息公开实施方案
2014/03/23 职场文书
奥巴马开学演讲稿
2014/05/15 职场文书
2014年体育工作总结
2014/11/24 职场文书
2019个人年度目标制定攻略!
2019/07/12 职场文书
小学五年级(说明文3篇)
2019/08/13 职场文书
PHP新手指南
2021/04/01 PHP
Go 在 MongoDB 中常用查询与修改的操作
2021/05/07 Golang
Node.js实现断点续传
2021/06/23 Javascript