DDR爱好者之家 Design By 杰米
本文实例为大家分享了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())
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]