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 相关文章推荐
Django框架中的对象列表视图使用示例
Jul 21 Python
详解duck typing鸭子类型程序设计与Python的实现示例
Jun 03 Python
python利用MethodType绑定方法到类示例代码
Aug 27 Python
使用python编写简单的小程序编译成exe跑在win10上
Jan 15 Python
详解Python3之数据指纹MD5校验与对比
Jun 11 Python
python sklearn库实现简单逻辑回归的实例代码
Jul 01 Python
详解基于python-django框架的支付宝支付案例
Sep 23 Python
django 实现celery动态设置周期任务执行时间
Nov 19 Python
python基于opencv实现人脸识别
Jan 04 Python
Python Spyder 调出缩进对齐线的操作
Feb 26 Python
pytorch 如何把图像数据集进行划分成train,test和val
May 31 Python
Python基本知识点总结
Apr 07 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中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
2016/01/07 PHP
laravel中的错误与日志用法详解
2016/07/26 PHP
PHP构造函数与析构函数用法示例
2016/09/28 PHP
js下通过getList函数实现分页效果的代码
2010/09/17 Javascript
js字符串转换成xml对象并使用技巧解读
2013/04/18 Javascript
分享33个jQuery与CSS3实现的绚丽鼠标悬停效果
2014/12/15 Javascript
Javascript数组操作函数总结
2015/02/05 Javascript
简单实现jQuery进度条轮播实例代码
2016/06/20 Javascript
js 判断登录界面的账号密码是否为空
2017/02/08 Javascript
Bootstrap 设置datetimepicker在屏幕上面弹出设置方法
2017/03/21 Javascript
javascript实现动态显示颜色块的报表效果
2017/04/10 Javascript
JavaScript实现自动跳转文本功能
2017/05/25 Javascript
Javascript实现的StopWatch功能示例
2017/06/13 Javascript
Three.JS实现三维场景
2018/12/30 Javascript
详解Howler.js Web音频播放终极解决方案
2020/08/23 Javascript
[48:38]DOTA2亚洲邀请赛 3.31 小组赛 B组 Mineski vs Secret
2018/03/31 DOTA
[55:35]VGJ.S vs Mski Supermajor小组赛C组 BO3 第二场 6.3
2018/06/04 DOTA
用Python的Django框架来制作一个RSS阅读器
2015/07/22 Python
Python中如何使用if语句处理列表实例代码
2019/02/24 Python
深入浅析Python中的迭代器
2019/06/04 Python
python获取txt文件词向量过程详解
2019/07/05 Python
python基于Selenium的web自动化框架
2019/07/14 Python
Python迭代器iterator生成器generator使用解析
2019/10/24 Python
Django rest framework分页接口实现原理解析
2020/08/21 Python
方太官方网上商城:销售方太抽油烟机、燃气灶、消毒柜等
2017/01/17 全球购物
联想澳大利亚官网:Lenovo Australia
2018/01/18 全球购物
消防安全检查制度
2014/02/04 职场文书
班级口号大全
2014/06/09 职场文书
文员求职信
2014/07/15 职场文书
办公楼租房协议书范本
2014/11/25 职场文书
悬空寺导游词
2015/02/05 职场文书
2015年反洗钱工作总结
2015/04/25 职场文书
公务员廉洁从政心得体会
2016/01/19 职场文书
Python批量将csv文件转化成xml文件的实例
2021/05/10 Python
pytorch DataLoader的num_workers参数与设置大小详解
2021/05/28 Python
Redis+AOP+自定义注解实现限流
2022/06/28 Redis