1. 程式人生 > >4. PyQt5-實現顯示和業務邏輯分離

4. PyQt5-實現顯示和業務邏輯分離

ini 圖片 顯示效果 com text rom qtgui 代碼 orm


PyQt5 實現顯示和業務邏輯分離, 繼承主界面文件的主窗口類

環境

操作系統: Windows 10 專業版 64-bit (10.0, Build 16299) (16299.rs3_release.170928-1534)
Python Version: 3.7.1
PyQt5 Version: 5.11.3
Qt Designer Version: 5.11.2

界面代碼如下(firstMainWin.py)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file ‘firstMainWin.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(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(220, 160, 141, 51))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        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", "haha"))
        self.pushButton.setText(_translate("MainWindow", "PushPush"))

主要邏輯代碼如下

‘‘‘
    PyQt5 實現顯示和業務邏輯分離, 繼承主界面文件的主窗口類
    @Author: Notus([email protected])
    @Create: 2019-02-10
    @Update: 2019-02-10
‘‘‘

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from firstMainWin import *

class MyMainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyMainWindow()
    myWin.show()
    sys.exit(app.exec_())

運行後顯示效果如下圖

技術分享圖片

4. PyQt5-實現顯示和業務邏輯分離