基于Python和PyQt5创建的嵌入式窗口

基于Python和PyQt5创建的嵌入式窗口

采用界面与逻辑分离的方式

界面文件和逻辑文件是两个相对独立的文件,通过上述方法就实现了界面与逻辑的分离

实现界面与逻辑的分离方法很简单,只需要新建一个xxxx.py文件,并继承界面文件的主窗口类

ui文件

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

# Form implementation generated from reading ui file 'ui_setup.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_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))

逻辑文件文件

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from ui_setup import *

# myWin类就是为了继承UI界面的
class myWin(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(myWin, self).__init__(parent)
        self.setupUi(self)


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

在逻辑文件中运行结果

基于Python和PyQt5创建的嵌入式窗口_第1张图片

采用ui与逻辑结合的方式

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QWidget

class winDemo(QWidget):
    def __init__(self):
        super(winDemo, self).__init__()
        self.setWindowTitle('创建一个空白界面')
        self.resize(300, 300)

        layout = QVBoxLayout()
        # 添加控件button
        self.btn1 = QPushButton(self)
        self.btn1.setText("这是一个按钮")
        layout.addWidget(self.btn1)


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


基于Python和PyQt5创建的嵌入式窗口_第2张图片

你可能感兴趣的:(python3)