Pyqt5實(shí)戰(zhàn)小案例之界面與邏輯分離的小計(jì)算器程序
直接看下最終效果:

使用技術(shù)總結(jié)
- 使用Designer設(shè)計(jì)界面
- 使用pyuic5命令導(dǎo)出到python文件
- 新建邏輯處理文件,繼承pyuic5導(dǎo)出的文件的類(lèi),在里面編寫(xiě)信號(hào)與槽的處理邏輯
使用Designer設(shè)計(jì)界面
要使用Designer,安裝一個(gè)Python庫(kù)即可:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyqt5 pyqt5-tools
我用的是python 3.9版本,發(fā)現(xiàn)3.10會(huì)報(bào)錯(cuò),3.9沒(méi)問(wèn)題

在此過(guò)程中,用到了如下組件:
- QLabel、QlineEdit、QPushButton
- 先從小范圍再到大范圍,進(jìn)行布局管理
- 按鈕的上下方,加上彈簧
然后保存成ui文件
使用pyuic5將ui文件導(dǎo)出成python文件

然后執(zhí)行如下命令:
pyuic5 -o computer.py computer.ui
即可生成computer.py
為什么要用界面與邏輯分離
主要是用Designer設(shè)計(jì)的界面,肯定要多次調(diào)整的。如果每次調(diào)整后,生成新的py文件,就會(huì)把自己寫(xiě)的代碼給覆蓋了。
因此,最好是界面的ui生成的python代碼,和自己的分離。
用繼承機(jī)制就可以:
先看下pyuic5生成的代碼文件(如下代碼是自動(dòng)生成的,不用多看):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'computer.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1006, 577)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(Form)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit_num01 = QtWidgets.QLineEdit(Form)
self.lineEdit_num01.setObjectName("lineEdit_num01")
self.horizontalLayout.addWidget(self.lineEdit_num01)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_num02 = QtWidgets.QLineEdit(Form)
self.lineEdit_num02.setObjectName("lineEdit_num02")
self.horizontalLayout_2.addWidget(self.lineEdit_num02)
self.verticalLayout.addLayout(self.horizontalLayout_2)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.pushButton_add = QtWidgets.QPushButton(Form)
self.pushButton_add.setObjectName("pushButton_add")
self.horizontalLayout_3.addWidget(self.pushButton_add)
self.pushButton_minus = QtWidgets.QPushButton(Form)
self.pushButton_minus.setObjectName("pushButton_minus")
self.horizontalLayout_3.addWidget(self.pushButton_minus)
self.pushButton_multi = QtWidgets.QPushButton(Form)
self.pushButton_multi.setObjectName("pushButton_multi")
self.horizontalLayout_3.addWidget(self.pushButton_multi)
self.pushButton_divide = QtWidgets.QPushButton(Form)
self.pushButton_divide.setObjectName("pushButton_divide")
self.horizontalLayout_3.addWidget(self.pushButton_divide)
self.verticalLayout.addLayout(self.horizontalLayout_3)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.label_result = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(18)
self.label_result.setFont(font)
self.label_result.setStyleSheet("QLabel{\n"
"color:red;\n"
"}")
self.label_result.setText("")
self.label_result.setObjectName("label_result")
self.verticalLayout.addWidget(self.label_result)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "請(qǐng)輸入第一個(gè)數(shù)字:"))
self.label_2.setText(_translate("Form", "請(qǐng)輸入第二個(gè)數(shù)字:"))
self.pushButton_add.setText(_translate("Form", "+"))
self.pushButton_minus.setText(_translate("Form", "-"))
self.pushButton_multi.setText(_translate("Form", "*"))
self.pushButton_divide.setText(_translate("Form", "/"))在里面有個(gè)類(lèi),叫做:Ui_Form
我們可以繼承這個(gè)類(lèi),然后編寫(xiě)編寫(xiě)自己的邏輯即可
import sys
from computer import Ui_Form
from PyQt5.QtWidgets import QApplication, QWidget
class MyUiComputer(Ui_Form):
def __init__(self, window):
super().__init__()
self.setupUi(window)
self.pushButton_add.clicked.connect(self.do_compute("+"))
self.pushButton_minus.clicked.connect(self.do_compute("-"))
self.pushButton_multi.clicked.connect(self.do_compute("*"))
self.pushButton_divide.clicked.connect(self.do_compute("/"))
def do_compute(self, method):
def func():
try:
num01 = self.lineEdit_num01.text()
num02 = self.lineEdit_num02.text()
if method == "+":
self.label_result.setText("計(jì)算結(jié)果:" + str(int(num01) + int(num02)))
elif method == "-":
self.label_result.setText("計(jì)算結(jié)果:" + str(int(num01) - int(num02)))
elif method == "*":
self.label_result.setText("計(jì)算結(jié)果:" + str(int(num01) * int(num02)))
elif method == "/":
self.label_result.setText("計(jì)算結(jié)果:" + str(int(num01) / int(num02)))
except Exception as e:
self.label_result.setText(f"error: {e}")
return func
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QWidget()
my_ui_computer = MyUiComputer(window)
window.show()
sys.exit(app.exec_())
這是我們自己的代碼,有幾點(diǎn):
1、需要自己初始化 QWidget 作為入口窗口 然后調(diào)用pyuic5生成代碼中的 self.setupUi(window) 設(shè)置主窗口;
2、這個(gè)代碼主要是編寫(xiě)自己的信號(hào)與槽的鏈接
3、這個(gè)代碼也有一個(gè)知識(shí),connect的槽函數(shù),是可以傳參數(shù)的,技巧就是自己的函數(shù)返回一個(gè)函數(shù) 見(jiàn)do_compute函數(shù)
總結(jié)
依然回顧下pyqt5開(kāi)發(fā)的流程:
用Designer設(shè)計(jì)界面
用pyuic5將ui文件導(dǎo)出成python文件
用繼承的方法,編寫(xiě)自己的類(lèi)文件,實(shí)現(xiàn)自己的信號(hào)處理邏輯
自己的代碼中,初始化入口的Window,傳給pyuic5的文件作為啟動(dòng)入口
到此這篇關(guān)于Pyqt5實(shí)戰(zhàn)小案例之界面與邏輯分離的小計(jì)算器程序的文章就介紹到這了,更多相關(guān)Pyqt5小計(jì)算器程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3 微信支付(小程序支付)V3接口的實(shí)現(xiàn)
本文主要介紹了Python3 微信支付(小程序支付)V3接口的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Python中列表和元組的相關(guān)語(yǔ)句和方法講解
這篇文章主要介紹了Python中列表和元組的相關(guān)語(yǔ)句和方法講解,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08
圖文講解選擇排序算法的原理及在Python中的實(shí)現(xiàn)
這篇文章主要介紹了選擇排序的原理及在Python中的實(shí)現(xiàn),選擇排序的時(shí)間復(fù)雜度為О(n²),需要的朋友可以參考下2016-05-05
python更換國(guó)內(nèi)鏡像源三種實(shí)用方法
這篇文章主要給大家介紹了關(guān)于python更換國(guó)內(nèi)鏡像源三種實(shí)用方法的相關(guān)資料,更換Python鏡像源可以幫助解決使用pip安裝包時(shí)速度過(guò)慢或無(wú)法連接的問(wèn)題,需要的朋友可以參考下2023-09-09
解決python的空格和tab混淆而報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決python的空格和tab混淆而報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Python爬蟲(chóng)學(xué)習(xí)之翻譯小程序
這篇文章主要為大家詳細(xì)介紹了Python爬蟲(chóng)學(xué)習(xí)之翻譯小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
用Python實(shí)現(xiàn)校園通知更新提醒功能
今天小編就為大家分享一篇用Python實(shí)現(xiàn)校園通知更新提醒功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

