环境:pycharm、python3.7
首先用qtDesigner设计出如下界面。
再用pyUIC将.ui文件转化内.py文件。源码如下
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'calculator.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # 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(726, 592) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.outLine = QtWidgets.QLineEdit(self.centralwidget) self.outLine.setGeometry(QtCore.QRect(180, 50, 351, 41)) self.outLine.setObjectName("outLine") self.bt1 = QtWidgets.QPushButton(self.centralwidget) self.bt1.setGeometry(QtCore.QRect(190, 110, 61, 61)) self.bt1.setObjectName("bt1") self.bt2 = QtWidgets.QPushButton(self.centralwidget) self.bt2.setGeometry(QtCore.QRect(270, 110, 61, 61)) self.bt2.setObjectName("bt2") self.bt3 = QtWidgets.QPushButton(self.centralwidget) self.bt3.setGeometry(QtCore.QRect(360, 110, 61, 61)) self.bt3.setObjectName("bt3") self.btPlus = QtWidgets.QPushButton(self.centralwidget) self.btPlus.setGeometry(QtCore.QRect(450, 110, 61, 61)) self.btPlus.setObjectName("btPlus") self.bt6 = QtWidgets.QPushButton(self.centralwidget) self.bt6.setGeometry(QtCore.QRect(360, 190, 61, 61)) self.bt6.setObjectName("bt6") self.bt5 = QtWidgets.QPushButton(self.centralwidget) self.bt5.setGeometry(QtCore.QRect(270, 190, 61, 61)) self.bt5.setObjectName("bt5") self.btSub = QtWidgets.QPushButton(self.centralwidget) self.btSub.setGeometry(QtCore.QRect(450, 190, 61, 61)) self.btSub.setObjectName("btSub") self.bt4 = QtWidgets.QPushButton(self.centralwidget) self.bt4.setGeometry(QtCore.QRect(190, 190, 61, 61)) self.bt4.setObjectName("bt4") self.bt9 = QtWidgets.QPushButton(self.centralwidget) self.bt9.setGeometry(QtCore.QRect(360, 270, 61, 61)) self.bt9.setObjectName("bt9") self.bt8 = QtWidgets.QPushButton(self.centralwidget) self.bt8.setGeometry(QtCore.QRect(270, 270, 61, 61)) self.bt8.setObjectName("bt8") self.btMul = QtWidgets.QPushButton(self.centralwidget) self.btMul.setGeometry(QtCore.QRect(450, 270, 61, 61)) self.btMul.setObjectName("btMul") self.bt7 = QtWidgets.QPushButton(self.centralwidget) self.bt7.setGeometry(QtCore.QRect(190, 270, 61, 61)) self.bt7.setObjectName("bt7") self.btCal = QtWidgets.QPushButton(self.centralwidget) self.btCal.setGeometry(QtCore.QRect(360, 340, 61, 61)) self.btCal.setObjectName("btCal") self.bt0 = QtWidgets.QPushButton(self.centralwidget) self.bt0.setGeometry(QtCore.QRect(270, 340, 61, 61)) self.bt0.setObjectName("bt0") self.btDiv = QtWidgets.QPushButton(self.centralwidget) self.btDiv.setGeometry(QtCore.QRect(450, 340, 61, 61)) self.btDiv.setObjectName("btDiv") self.btClr = QtWidgets.QPushButton(self.centralwidget) self.btClr.setGeometry(QtCore.QRect(190, 340, 61, 61)) self.btClr.setObjectName("btClr") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 726, 26)) 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.bt1.setText(_translate("MainWindow", "1")) self.bt2.setText(_translate("MainWindow", "2")) self.bt3.setText(_translate("MainWindow", "3")) self.btPlus.setText(_translate("MainWindow", "+")) self.bt6.setText(_translate("MainWindow", "6")) self.bt5.setText(_translate("MainWindow", "5")) self.btSub.setText(_translate("MainWindow", "-")) self.bt4.setText(_translate("MainWindow", "4")) self.bt9.setText(_translate("MainWindow", "9")) self.bt8.setText(_translate("MainWindow", "8")) self.btMul.setText(_translate("MainWindow", "*")) self.bt7.setText(_translate("MainWindow", "7")) self.btCal.setText(_translate("MainWindow", "=")) self.bt0.setText(_translate("MainWindow", "0")) self.btDiv.setText(_translate("MainWindow", "/")) self.btClr.setText(_translate("MainWindow", "clear"))
在新建一个py文件引入刚才pyUIC生成的ui文件。写入相关代码,源码如下:
# -*- coding: utf-8 -*- # @Author:ct # @ProjectName:calculator.py # @Desc: # @Time: import sys from calculatorUI import Ui_MainWindow from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow class mWindow(QMainWindow, Ui_MainWindow): def __init__(self): super(mWindow, self).__init__() self.setupUi(self) # 定义所有按钮事件函数 def prs_bt0(self): self.outLine.insert('0') def prs_bt1(self): self.outLine.insert('1') def prs_bt2(self): self.outLine.insert('2') def prs_bt3(self): self.outLine.insert('3') def prs_bt4(self): self.outLine.insert('4') def prs_bt5(self): self.outLine.insert('5') def prs_bt6(self): self.outLine.insert('6') def prs_bt7(self): self.outLine.insert('7') def prs_bt8(self): self.outLine.insert('8') def prs_bt9(self): self.outLine.insert('9') def prs_btPlus(self): self.outLine.insert('+') def prs_btSub(self): self.outLine.insert('-') def prs_btMul(self): self.outLine.insert('*') def prs_btDiv(self): self.outLine.insert('/') def prs_btClr(self): self.outLine.clear() # 计算表达式事件,通过eval函数将字符串表达式计算 def prs_btCal(self): # 获得输入框的内容(字符串形式) exp = self.outLine.text() res = eval(exp) self.outLine.insert('=') self.outLine.insert(str(res)) if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = mWindow() # mainWindow.outLine.setEnabled(False) # 使输出框只能被读取,不能输入内容 mainWindow.outLine.setReadOnly(True) # 关联所有按钮事件 mainWindow.bt0.clicked.connect(mainWindow.prs_bt0) mainWindow.bt1.clicked.connect(mainWindow.prs_bt1) mainWindow.bt2.clicked.connect(mainWindow.prs_bt2) mainWindow.bt3.clicked.connect(mainWindow.prs_bt3) mainWindow.bt4.clicked.connect(mainWindow.prs_bt4) mainWindow.bt5.clicked.connect(mainWindow.prs_bt5) mainWindow.bt6.clicked.connect(mainWindow.prs_bt6) mainWindow.bt7.clicked.connect(mainWindow.prs_bt7) mainWindow.bt8.clicked.connect(mainWindow.prs_bt8) mainWindow.bt9.clicked.connect(mainWindow.prs_bt9) mainWindow.btPlus.clicked.connect(mainWindow.prs_btPlus) mainWindow.btDiv.clicked.connect(mainWindow.prs_btDiv) mainWindow.btMul.clicked.connect(mainWindow.prs_btMul) mainWindow.btSub.clicked.connect(mainWindow.prs_btSub) mainWindow.btCal.clicked.connect(mainWindow.prs_btCal) mainWindow.btClr.clicked.connect(mainWindow.prs_btClr) mainWindow.show() # 让程序循环运行 sys.exit(app.exec_())
运行程序,结果如下:
可进行简单的运算。当然还有诸多bug,如输入错误表达式不能容错等,待以后修复。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理