当前位置: 首页 > article >正文

PyQt加载UI文件


1.动态加载

import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *
from PySide6.QtUiTools import QUiLoader


class readfile(QWidget):
    def __init__(self):
        super().__init__()
        self.ui=QUiLoader().load("test.ui",self)       
        self.__create_connections()

    def __create_connections(self):              
        self.ui.pushButton.clicked.connect(self.open_file)        


    def open_file(self):        
        fname,_ = QFileDialog.getOpenFileName(self,"打开文件", '.')
        if fname:            
            self.ui.lineEdit.setText(fname)            
            with open(fname,'r') as f:
            	content = f.read()
            self.ui.textBrowser.setText(str(content))     
if __name__ == '__main__':
	#QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
	#QApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
	app = QApplication(sys.argv)
	w = readfile()
	w.show()       
	sys.exit(app.exec())

2.使用QFile加载

import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *
from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader


class readfile(QWidget):
    def __init__(self):
        super().__init__()
        qfile=QFile("test.ui")
        qfile.open(QFile.ReadOnly)
        qfile.close()
        self.ui=QUiLoader().load(qfile,self)      
        self.__create_connections()

    def __create_connections(self):              
        self.ui.pushButton.clicked.connect(self.open_file)        


    def open_file(self):        
        fname,_ = QFileDialog.getOpenFileName(self,"打开文件", '.')
        if fname:            
            self.ui.lineEdit.setText(fname)            
            with open(fname,'r') as f:
            	content = f.read()
            self.ui.textBrowser.setText(str(content))     
if __name__ == '__main__':
	#QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
	#QApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
	app = QApplication(sys.argv)
	w = readfile()
	w.show()       
	sys.exit(app.exec())

动态加载出有错误信息,但不影响运行
错误信息如下:

qt.pysideplugin: Environment variable PYSIDE_DESIGNER_PLUGINS is not set, bailing out.
qt.pysideplugin: No instance of QPyDesignerCustomWidgetCollection was found.
Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute and QSGRendererInterface::OpenGLRhi using QQuickWindow::setGraphicsApi before constructing QGuiApplication.

 


http://www.kler.cn/a/552677.html

相关文章:

  • HarmonyOS NEXT 5.0.0.126 最新升级内容详解
  • 浅谈模组-相机鬼像
  • 【面试题系列】Java 多线程面试题深度解析
  • 硕成C语言24
  • 【核心算法篇二】《DeepSeek NLP实战:BERT/GPT/LLM全系调优》
  • MySQL5.7 创建用户并授予超管权限脚本
  • 在 Ubuntu 22.04 中修改主机名称(hostname)
  • Neo4j集群学习
  • 开源在线考试系统开源在线考试系统:支持数学公式的前后端分离解决方案
  • 2025最新智能优化算法:改进型雪雁算法(Improved Snow Geese Algorithm, ISGA)求解23个经典函数测试集,MATLAB
  • Java 面试篇-Redis 专题(Redis 常见的面试专题:缓存击穿、缓存雪崩、缓存穿透、什么是布隆过滤器、什么是延时双删、持久化的方式、Redis 分布式锁、I/O 多路复用等等)
  • ​实在智能与宇树科技、云深科技一同获评浙江省“人工智能服务商”、 “数智优品”​等荣誉
  • Linux-权限维持
  • Go入门之流程控制
  • HTTP FTP SMTP TELNET 应用协议
  • Farewell Go,Hello AI:是时候说再见了
  • 202305 青少年软件编程等级考试C/C++ 三级真题答案及解析(电子学会)
  • 在unity中实现隐藏窗口,显示系统托盘图标,右键菜单退出功能
  • 怎么把pyqt界面做的像web一样漂亮
  • Cherno C++ P54 内存:栈与堆