PyQt5实现简易计算器
作者:数学工具构造器 发布时间:2022-12-14 02:12:38
标签: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())
来源:https://blog.csdn.net/TQCAI666/article/details/80532034
0
投稿
猜你喜欢
- 简单的模型例如线性回归,LR等模型非常易于解释,但在实际应用中的效果却远远低于复杂的梯度提升树模型以及神经网络等模型。现在大部分互联网公司的
- [Q]怎么样查询特殊字符,如通配符%与_ [Q]如何插入单引号到数据库表中 [Q]怎样设置事务一致性 [Q]怎么样利用光标更新数据 [Q]怎
- 前言:本文从0到1实现django搭建一个web页面,实现了django连接mysql数据库,简单的get、post请求等一、使用pyach
- 本文实例讲述了Python表示矩阵的方法。分享给大家供大家参考,具体如下:在c语言中,表示个“整型3行4列”的矩阵,可以这样声明:int&n
- 链判断运算符(?.)非常好用、常用,搭配Null 判断运算符使用,效果更佳,完美!来,上代码:我们通常获取一个对象多层的属性值时,需要进行多
- 这篇文章主要介绍了python批量启动多线程代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 一:使用layer.open打开的子页面window.parent.location.reload()//刷新父页面var index =
- 如下所示:<html xmlns="http://www.w3.org/1999/xhtml"><he
- 简介在生产环境上,为了避免数据的丢失,通常情况下都会定时的对数据库进行备份。而Linux的crontab指令则可以帮助我们实现对数据库定时进
- PHP下载图片后文件打开显示损坏问题用php写个图片下载方法,测试发现下载的图片大小都没问题,但是无法打开文件。解决方法如下:首先打开文件下
- 此类技巧还有很多,欢迎继续分享解析 URL从 James Padolsey 的 Blog中看到的个小技巧,就是利用 a 标签的 DOM 属性
- 1、更新整张表,如果某一行数据的某一列的值为空,那么让他和另一列字段的值相等UPDATE ot_tgbz set update_day=if
- 直接通过element-ui自带的上传组件结合js即可,代码如下:HTML: &l
- Tips:微信小程序可以在HbuilderX用HTML标签(如Ddiv、span等)写前端代码,也可以用微信小程序语法写(view、swip
- 何为共线性:共线性问题指的是输入的自变量之间存在较高的线性相关度。共线性问题会导致回归模型的稳定性和准确性大大降低,另外,过多无关的维度计算
- FrontPage服务器扩展可以做以下工作:(1),保存和检索Web服务器的文件;(2),远程执行所有文件管理操作;(3),改名或移动文件时
- 今天服务器重新安装mssql2005版本,为了安全让mssql2005运行在独立用户下,权限也没错误,但竟然出现了以下错误; -------
- 一、石头剪刀布游戏目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机PK。如果游戏者赢了,得分就会添加,直到结束游戏
- $r = new ReflectionClass($this); Zend_Debug::dump($r->getConstants(
- 这个分页使用的是0游标,也就是Rs.Open Sql,Conn,0,1。但是感觉也快不了多少,10万条数据的分页时间300多豪秒之间。风格A