PySide6直接载入UI文件实现界面和代码分离
时间:2021-09-10
本文章向大家介绍PySide6直接载入UI文件实现界面和代码分离,主要包括PySide6直接载入UI文件实现界面和代码分离使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
直接载入,无需再生成相应的py文件。
参考:Using .ui files from Designer or QtCreator with QUiLoader and pyside6-uic — Qt for Python
入口文件 simpleApp.py
from mainWindow import Window from qt import QApplication, QIcon, _exec if __name__ == '__main__': app = QApplication([]) app.setWindowIcon(QIcon("logo.png")) # 添加图标 w = Window() w.ui.show() #w.win.show() _exec(app)
界面控件属性设置文件 mainWindow.py
import sys from PySide6.QtGui import QScreen from PySide6.QtCore import QFile from PySide6.QtUiTools import QUiLoader from PySide6.QtWidgets import QApplication, QButtonGroup, QLabel, QMessageBox class Window: def __init__(self): super(Window, self).__init__() # 从ui文件中加载UI定义 qfile = QFile("mainPage.ui") qfile.open(QFile.ReadOnly) qfile.close() # 从UI定义中动态创建一个相应的窗口对象 self.ui = QUiLoader().load(qfile) if not self.ui: print(QUiLoader().errorString()) sys.exit(-1) self.center() # 信号处理 self.ui.pushButton.clicked.connect(self.btnClick) # self.button.clicked.connect(self.btnClick) def btnClick(self): info = self.ui.textEdit.toPlainText() # 获取文本信息 # info = self.textEdit.toPlainText() print(info) # self.appLabel = QLabel() # self.appLabel.setAlignment(Qt.AlignCenter) # self.appLabel.alignment = Qt.AlignCenter # self.appLabel.setText(info) # self.appLabel.setWindowTitle(info) # self.appLabel.setGeometry(300, 300, 250, 175) # self.ui.setWindowTitle(info) # # 显示这个Label # self.appLabel.show() self.msgbox = QMessageBox() msgBox = self.msgbox msgBox.setText(info) # msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel) # msgBox.setDefaultButton(QMessageBox.Save) msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Discard | QMessageBox.Cancel) msgBox.setIcon(QMessageBox.Information) msgBox.setDetailedText("Information:" "\nsdfsdfaaa" "\nTestsertsetsetse") msgBox.setInformativeText("sdfsdfasssssss") # msgBox.show() # MessageBox = QMessageBox() # Ret = MessageBox.question(self.ui, "标题", "内容") #Critical对话框 # print(Ret) self.msgbox2 = QMessageBox() msgBox2 = self.msgbox2 connectButton = msgBox2.addButton("Connect", QMessageBox.ActionRole) disconnectButton = msgBox2.addButton("DisConnect", QMessageBox.ActionRole) msgBox.setText(info) msgBox2.setIcon(QMessageBox.Information) msgBox2.exec() if msgBox2.clickedButton() == connectButton: self.ui.textEdit.append("Connect clicked!") print("Connect pass") elif msgBox2.clickedButton() == disconnectButton: self.ui.textEdit.append("DisConnect clicked!") print("DisConnect pass") def center(self): #Get Screen geometry SrcSize = QScreen.availableGeometry(QApplication.primaryScreen()) #Set X Position Center frmX = (SrcSize.width() - self.ui.width())/2 #Set Y Position Center frmY = (SrcSize.height() - self.ui.height())/2 #Set Form's Center Location self.ui.move(frmX, frmY)
界面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>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QGroupBox" name="groupBox"> <property name="geometry"> <rect> <x>40</x> <y>10</y> <width>431</width> <height>451</height> </rect> </property> <property name="title"> <string>GroupBox</string> </property> <widget class="QRadioButton" name="radioButton"> <property name="geometry"> <rect> <x>40</x> <y>80</y> <width>95</width> <height>20</height> </rect> </property> <property name="text"> <string>RadioButton</string> </property> </widget> <widget class="QCheckBox" name="checkBox"> <property name="geometry"> <rect> <x>40</x> <y>20</y> <width>81</width> <height>20</height> </rect> </property> <property name="text"> <string>CheckBox</string> </property> </widget> <widget class="QTabWidget" name="tabWidget"> <property name="geometry"> <rect> <x>40</x> <y>110</y> <width>351</width> <height>261</height> </rect> </property> <property name="currentIndex"> <number>0</number> </property> <widget class="QWidget" name="tab"> <attribute name="title"> <string>Tab 1</string> </attribute> <widget class="QTextEdit" name="textEdit"> <property name="geometry"> <rect> <x>10</x> <y>30</y> <width>331</width> <height>191</height> </rect> </property> </widget> </widget> <widget class="QWidget" name="tab_2"> <attribute name="title"> <string>Tab 2</string> </attribute> </widget> </widget> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>330</x> <y>410</y> <width>93</width> <height>28</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QRadioButton" name="radioButton_2"> <property name="geometry"> <rect> <x>40</x> <y>50</y> <width>95</width> <height>20</height> </rect> </property> <property name="text"> <string>RadioButton</string> </property> </widget> </widget> <widget class="QToolBox" name="toolBox"> <property name="geometry"> <rect> <x>480</x> <y>20</y> <width>291</width> <height>201</height> </rect> </property> <property name="currentIndex"> <number>1</number> </property> <widget class="QWidget" name="page"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>291</width> <height>141</height> </rect> </property> <attribute name="label"> <string>Page 1</string> </attribute> </widget> <widget class="QWidget" name="page_2"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>291</width> <height>141</height> </rect> </property> <attribute name="label"> <string>Page 2</string> </attribute> </widget> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>22</height> </rect> </property> <widget class="QMenu" name="menuMenu"> <property name="title"> <string>File</string> </property> <addaction name="actionOpen"/> <addaction name="actionSave"/> <addaction name="actionClose"/> </widget> <widget class="QMenu" name="menuEdit"> <property name="title"> <string>Edit</string> </property> <addaction name="actionSettings"/> <addaction name="actionReload"/> </widget> <addaction name="menuMenu"/> <addaction name="menuEdit"/> </widget> <widget class="QStatusBar" name="statusbar"/> <action name="actionOpen"> <property name="text"> <string>Open</string> </property> </action> <action name="actionSave"> <property name="text"> <string>Save</string> </property> </action> <action name="actionClose"> <property name="text"> <string>Close</string> </property> </action> <action name="actionSettings"> <property name="text"> <string>Settings</string> </property> </action> <action name="actionReload"> <property name="text"> <string>Reload</string> </property> </action> </widget> <resources/> <connections/> </ui>
以下文件用于扩展相关功能,但是入口文件中会用到,需要import:
PyQt6和PySide6兼容设置 qt.py:
import sys if 'PyQt6' in sys.modules: # PyQt6 from PyQt6 import QtGui, QtWidgets, QtCore from PyQt6.QtCore import pyqtSignal as Signal, pyqtSlot as Slot else: # PySide6 from PySide6 import QtGui, QtWidgets, QtCore from PySide6.QtCore import Signal, Slot from PySide6.QtWidgets import QApplication from PySide6.QtGui import QIcon def _enum(obj, name): parent, child = name.split('.') result = getattr(obj, child, False) if result: # Found using short name only. return result obj = getattr(obj, parent) # Get parent, then child. return getattr(obj, child) def _exec(obj): if hasattr(obj, 'exec'): return obj.exec() else: return obj.exec_()
打包部署需要用到的,实现dll文件整理到dist\lib文件夹下(试了一下功能未实现,仅做记录,寻找解决方案,如有解决方案的朋友请不吝赐教)
import sys import os currentdir = os.path.dirname(sys.argv[0]).replace("/","\\") libdir = os.path.join(currentdir, "lib") sys.path.append(libdir) os.environ['path'] += './lib' print(sys.path) print(f"\n{os.environ['path']}")
调用命令,结束后需要拷贝相应的UI文件到生成的目录:
pyinstaller simpleapp.py --noconsole --hidden-import PySide6.QtXml --workpath D:\Projects\PythonAPP\SimpleAPP\build --distpath D:\Projects\PythonAPP\SimpleAPP\build\dist --runtime-hook="runtimehook.py"
原文地址:https://www.cnblogs.com/CCJVL/p/15250607.html
- React第三方组件2(状态管理之Refast的使用③扩展ctx)
- 财经小知识——CRS风暴与全球离岸金融中心
- The Triangle
- React第三方组件2(状态管理之Refast的使用②异步修改state)
- UVa Automatic Editing
- React第三方组件2(状态管理之Refast的使用①简单使用)
- uva Excuses, Excuses!
- React第三方组件5(状态管理之Redux的使用④TodoList下)
- 2017/6/9-Python文件读写的方法
- React第三方组件5(状态管理之Redux的使用③TodoList中)
- R语言在收入不平等指标测度上的应用~
- React第三方组件5(状态管理之Redux的使用②TodoList上)
- sscanf
- sprintf的用法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- JSON解析问题:net.sf.json.JSONException: There is a cycle in the hierarchy!
- 海康相机SDK联合c++标定
- 开发一个Node命令行小玩具全过程--高颜统计工具
- 消息未读之点不完的小红点(Node+Websocket)
- pkg版本规范管理自动化最佳实践
- 数据库驱动问题:MySQLNonTransientConnectionException
- 喜大普奔,es2019登场
- [译] Object.assign 和 Object Spread 之争, 用谁?
- 急速 debug 实战二(浏览器 - 调试线上篇)
- via the 'serverTimezone' configuration property
- 急速 debug 实战一(浏览器-基础篇)
- MongoDB系列一: Replica Set 集群搭建实战
- 函数式编程看React Hooks(一)简单React Hooks实现
- 函数式编程看React Hooks(二)事件绑定副作用深度剖析
- Vue 开发必须知道的 36 个技巧【近1W字】