本文记录了 Python 安装 Pyside6 库,以及载入/读取动态 .ui
文件的相关代码。
前言
以下为 PyPI 网站对 Pyside6 的介绍。
PySide6 is the official Python module from the Qt for Python project, which provides access to the complete Qt 6.0+ framework.
Pyside6 提供了两种读取 UI 文件的方法,本文仅讲解动态载入 UI 文件的方法。
前期准备
此部分为 Pyside6 库动态读取 .ui
文件前所需要做的准备。
安装 Pyside6
- 执行以下命令安装 Pyside6
pip install pyside6==6.4.1
准备 .ui
文件
- 新建一个
mainwindow.ui
文件,将以下代码复制粘贴至其中,保存后退出<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>110</x> <y>80</y> <width>201</width> <height>81</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>20</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
一般来说
.ui
文件是由 Qt Designer 生成的,这里为了方便直接创建文件作为例子。
动态读取
此部分正式开始讲解 Pyside6 动态读取 .ui
文件的方法。
普通示例
- 首先,导入读取
.ui
文件所需要的库from PySide6.QtUiTools import QUiLoader
- 然后使用以下代码动态读取和显示
mainwindow.ui
文件中的 UI 界面ui_file = QFile("mainwindow.ui") ui_file.open(QFile.ReadOnly) loader = QUiLoader() window = loader.load(ui_file) window.show()
- 更完整、更高大上的代码如下
import sys from PySide6.QtUiTools import QUiLoader from PySide6.QtWidgets import QApplication from PySide6.QtCore import QFile, QIODevice if __name__ == "__main__": app = QApplication(sys.argv) ui_file_name = "mainwindow.ui" ui_file = QFile(ui_file_name) if not ui_file.open(QIODevice.ReadOnly): print(f"Cannot open {ui_file_name}: {ui_file.errorString()}") sys.exit(-1) loader = QUiLoader() window = loader.load(ui_file) ui_file.close() if not window: print(loader.errorString()) sys.exit(-1) window.show() sys.exit(app.exec())
函数封装
- 如果有多个
.ui
文件,建议把以上的代码封装为函数复用import sys from PySide6.QtUiTools import QUiLoader from PySide6.QtCore import QFile from PySide6.QtCore import QIODevice def ui_file_loading(file): ui_file_name = file ui_file = QFile(ui_file_name) if not ui_file.open(QIODevice.ReadOnly): print(f"Cannot open {ui_file_name}: {ui_file.errorString()}") sys.exit(-1) loader = QUiLoader() window = loader.load(ui_file) ui_file.close() if not window: print(loader.errorString()) sys.exit(-1) return window if __name__ == '__main__': from PySide6.QtWidgets import QApplication app = QApplication(sys.argv) w = ui_file_loading(r'./mainwindow.ui') w.show() sys.exit(app.exec())
我用 Pyside6
6.6.2
的版本运行以上代码却无法显示 UI 界面,但6.4.1
的版本一切正常。
总结
文中的代码大部分都是直接从官网复制过来的。
可以直接使用我封装为函数的代码。