PyQt5 Qt Designer使用(界面显示与业务逻辑分离模式)

PyQt5 QtDesigner使用


采用Qt Designer设计开发应用程序至少需要:xxx.ui(Qt Designer创建的ui文件),xxx.py(xxx.ui文件通过pyuic转换生产的python代码),和Call_xxx.py(业务逻辑代码)等三个文件。
所有的UI界面设置都放在xxx.ui这样UI界面的显示和业务逻辑的代码是完全分开独立的,开发UI界面比较复杂的程序建议采用这种方式。

Qt Designer运行界面:
在这里插入图片描述

MainWinSignalSlog02.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1032</width>
    <height>212</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <property name="toolTip">
   <string/>
  </property>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>40</y>
     <width>641</width>
     <height>151</height>
    </rect>
   </property>
   <property name="title">
    <string>打印控制</string>
   </property>
   <widget class="QPushButton" name="pushButton_close">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>100</y>
      <width>93</width>
      <height>28</height>
     </rect>
    </property>
    <property name="text">
     <string>关闭</string>
    </property>
   </widget>
   <widget class="Line" name="line">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>80</y>
      <width>551</width>
      <height>16</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Horizontal</enum>
    </property>
   </widget>
   <widget class="Line" name="line_2">
    <property name="geometry">
     <rect>
      <x>243</x>
      <y>90</y>
      <width>20</width>
      <height>51</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Vertical</enum>
    </property>
   </widget>
   <widget class="QWidget" name="">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>30</y>
      <width>551</width>
      <height>30</height>
     </rect>
    </property>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QLabel" name="label">
       <property name="text">
        <string>打印份数:</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QSpinBox" name="numberSpinBox"/>
     </item>
     <item>
      <widget class="QComboBox" name="styleCombo">
       <property name="currentIndex">
        <number>1</number>
       </property>
       <item>
        <property name="text">
         <string>A3</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>A4</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>A5</string>
        </property>
       </item>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeType">
        <enum>QSizePolicy::Fixed</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QLabel" name="label_2">
       <property name="text">
        <string>纸张类型:</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPushButton" name="printButton">
       <property name="text">
        <string>打印</string>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
   <widget class="QWidget" name="">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>100</y>
      <width>189</width>
      <height>30</height>
     </rect>
    </property>
    <layout class="QHBoxLayout" name="horizontalLayout_2">
     <item>
      <widget class="QCheckBox" name="previewStatus">
       <property name="text">
        <string>全屏预览</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPushButton" name="previewButton">
       <property name="text">
        <string>预览</string>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QGroupBox" name="groupBox_op">
   <property name="geometry">
    <rect>
     <x>700</x>
     <y>40</y>
     <width>311</width>
     <height>151</height>
    </rect>
   </property>
   <property name="title">
    <string>操作结果</string>
   </property>
   <widget class="QLabel" name="resultLabel">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>30</y>
      <width>271</width>
      <height>101</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>pushButton_close</sender>
   <signal>clicked()</signal>
   <receiver>Form</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>113</x>
     <y>228</y>
    </hint>
    <hint type="destinationlabel">
     <x>100</x>
     <y>303</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

Ui_MainWinSignalSlog02.py

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

# Form implementation generated from reading ui file 'd:\project\python\pyqt5\designerDemo\MainWinSignalSlog02.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(1032, 212)
        Form.setToolTip("")
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setGeometry(QtCore.QRect(30, 40, 641, 151))
        self.groupBox.setObjectName("groupBox")
        self.pushButton_close = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_close.setGeometry(QtCore.QRect(290, 100, 93, 28))
        self.pushButton_close.setObjectName("pushButton_close")
        self.line = QtWidgets.QFrame(self.groupBox)
        self.line.setGeometry(QtCore.QRect(30, 80, 551, 16))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.line_2 = QtWidgets.QFrame(self.groupBox)
        self.line_2.setGeometry(QtCore.QRect(243, 90, 20, 51))
        self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.widget = QtWidgets.QWidget(self.groupBox)
        self.widget.setGeometry(QtCore.QRect(30, 30, 551, 30))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.widget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.numberSpinBox = QtWidgets.QSpinBox(self.widget)
        self.numberSpinBox.setObjectName("numberSpinBox")
        self.horizontalLayout.addWidget(self.numberSpinBox)
        self.styleCombo = QtWidgets.QComboBox(self.widget)
        self.styleCombo.setObjectName("styleCombo")
        self.styleCombo.addItem("")
        self.styleCombo.addItem("")
        self.styleCombo.addItem("")
        self.horizontalLayout.addWidget(self.styleCombo)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.label_2 = QtWidgets.QLabel(self.widget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        self.printButton = QtWidgets.QPushButton(self.widget)
        self.printButton.setObjectName("printButton")
        self.horizontalLayout.addWidget(self.printButton)
        self.widget1 = QtWidgets.QWidget(self.groupBox)
        self.widget1.setGeometry(QtCore.QRect(30, 100, 189, 30))
        self.widget1.setObjectName("widget1")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.previewStatus = QtWidgets.QCheckBox(self.widget1)
        self.previewStatus.setObjectName("previewStatus")
        self.horizontalLayout_2.addWidget(self.previewStatus)
        self.previewButton = QtWidgets.QPushButton(self.widget1)
        self.previewButton.setObjectName("previewButton")
        self.horizontalLayout_2.addWidget(self.previewButton)
        self.groupBox_op = QtWidgets.QGroupBox(Form)
        self.groupBox_op.setGeometry(QtCore.QRect(700, 40, 311, 151))
        self.groupBox_op.setObjectName("groupBox_op")
        self.resultLabel = QtWidgets.QLabel(self.groupBox_op)
        self.resultLabel.setGeometry(QtCore.QRect(20, 30, 271, 101))
        self.resultLabel.setText("")
        self.resultLabel.setObjectName("resultLabel")

        self.retranslateUi(Form)
        self.styleCombo.setCurrentIndex(1)
        self.pushButton_close.clicked.connect(Form.close)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.groupBox.setTitle(_translate("Form", "打印控制"))
        self.pushButton_close.setText(_translate("Form", "关闭"))
        self.label.setText(_translate("Form", "打印份数:"))
        self.styleCombo.setItemText(0, _translate("Form", "A3"))
        self.styleCombo.setItemText(1, _translate("Form", "A4"))
        self.styleCombo.setItemText(2, _translate("Form", "A5"))
        self.label_2.setText(_translate("Form", "纸张类型:"))
        self.printButton.setText(_translate("Form", "打印"))
        self.previewStatus.setText(_translate("Form", "全屏预览"))
        self.previewButton.setText(_translate("Form", "预览"))
        self.groupBox_op.setTitle(_translate("Form", "操作结果"))

CallMainWinSignalSlog02.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal, Qt
from Ui_MainWinSignalSlog02 import Ui_Form


class MyMainWindow(QMainWindow, Ui_Form):
    helpSignal = pyqtSignal(str)
    printSignal = pyqtSignal(list)

    # 声明一个多重载版本的信号,包括一个带int和str类型参数的信号,以及带str类型参数的信号
    previewSignal = pyqtSignal([int, str], [str])

    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.initUI()

    def initUI(self):
        self.helpSignal.connect(self.showHelpMessage)
        self.printSignal.connect(self.printPaper)
        self.previewSignal[str].connect(self.previewPaper)
        self.previewSignal[int, str].connect(self.previewPaperWithArgs)
        self.printButton.clicked.connect(self.emitPrintSignal)
        self.previewButton.clicked.connect(self.emitPrivewSignal)

    # 发射预览信号
    def emitPrivewSignal(self):
        if self.previewStatus.isChecked() == True:
            self.previewSignal[int, str].emit(1080, " Full Screen")
        elif self.previewStatus.isChecked() == False:
            self.previewSignal[str].emit("PreView")
        pass
    
    # 发射打印信号
    def emitPrintSignal(self):
        pList = []
        pList.append(self.numberSpinBox.value())
        pList.append(self.styleCombo.currentText())
        self.printSignal.emit(pList)

    def printPaper(self, list):
        self.resultLabel.setText("打印:" + "份数:" + str(list[0]) + "纸张:" +str(list[1]))

    def previewPaperWithArgs(self, style, text):
        self.resultLabel.setText(str(style) + text)

    def previewPaper(self, text):
        self.resultLabel.setText(text)

    # 重载按键事件
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_F1:
            self.helpSignal.emit("help message")

    # 显示帮助信息
    def showHelpMessage(self, message):
        self.resultLabel.setText(message)
        self.statusBar().showMessage(message)        

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5是一个Python绑定Qt库的工具包,可以用于创建桌面应用程序。Qt Designer是一个用于创建Qt界面的可视化工具,可以方便地创建GUI界面并导出为.ui文件。 要使用Qt Designer编写PyQt5界面,可以按照以下步骤进行操作: 1. 安装PyQt5Qt Designer 如果你还没有安装PyQt5Qt Designer,可以使用以下命令进行安装: ``` pip install PyQt5 pyqt5-tools ``` 2. 创建Qt Designer界面 打开Qt Designer,创建一个新的界面。 3. 设计界面 在Qt Designer中,你可以从工具箱中拖拽控件到界面中,设置控件的属性,布局等。 4. 保存界面 在Qt Designer中,选择“文件”->“保存”,将界面保存为.ui文件。 5. 将.ui文件转换为.py文件 使用以下命令将.ui文件转换为.py文件: ``` pyuic5 -o ui_filename.py ui_filename.ui ``` 其中,ui_filename是你的.ui文件名。这将生成一个.py文件,其中包含Qt Designer界面的Python代码。 6. 编写程序 在Python代码中导入生成的.py文件,然后使用它来创建GUI界面。 下面是一个简单的示例程序: ```python from PyQt5 import QtWidgets, uic class MainWindow(QtWidgets.QMainWindow): def __init__(self): super(MainWindow, self).__init__() # Load the ui file uic.loadUi('ui_filename.ui', self) if __name__ == '__main__': app = QtWidgets.QApplication([]) window = MainWindow() window.show() app.exec_() ``` 在此示例中,我们使用`uic.loadUi`方法将.ui文件加载到`MainWindow`类中,然后创建`QApplication`和`MainWindow`实例,并将窗口显示出来。 以上就是使用Qt Designer编写PyQt5界面的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值