首页 > 其他分享 >PyQt加载UI文件

PyQt加载UI文件

时间:2023-02-04 17:14:08浏览次数:31  
标签:__ self PyQt PySide6 ui fname import 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.

标签:__,self,PyQt,PySide6,ui,fname,import,UI,加载
From: https://www.cnblogs.com/conpi/p/17091940.html

相关文章

  • 小米MIUI系统组件监听用户隐私
     欢迎关注微信公众号专注于网络安全领域,跟踪漏洞动态,深耕互联网,做一个深谙攻防之道的公众号。同时涉足多个领域,是哲学,抑或是文学与艺术,关注金融市场,研究全......
  • .net 7 中使用quic示例
    之前在文章在.Net中使用Quic通信尝鲜中介绍过如何使用quic协议,在.net7中,Quic相关API已经正式可用了,不过目前还是预览状态,基本示例如下:服务端代码:usingSystem;usin......
  • Cesium之影像底图加载
    1.引言Cesium是一款三维地球和地图可视化开源JavaScript库,使用WebGL来进行硬件加速图形,使用时不需要任何插件支持,基于Apache2.0许可的开源程序,可以免费用于商业和非商业......
  • 2023网络爬虫 -- 获取动态加载数据
    1、爬取的网址http://www.kfc.com.cn/kfccda/storelist/index.aspx2、要爬取的内容,输入关键字,点击查询,获取餐厅名称和餐厅地址3、F12,打开开发者工具,点击查询,抓包4、点......
  • 0-4 UUID
    UUID的版本UUID具有多个版本,每个版本的算法不同,应用范围也不同。首先是一个特例--NilUUID--通常我们不会用到它,它是由全为0的数字组成,如下:00000000-0000-0000-0000-000000......
  • PyQT调用ui界面文件
    通过QtDesigner将ui文件转存为py文件不继承Ui_FormimportsysfromPySide6importQtCore,QtWidgetsfromPySide6.QtWidgetsimport*fromui_testimportUi_Form......
  • uintptr & unsafe.Pointer
    uintptr&unsafe.Pointerunsafe.Pointer从名字来看它是不安全的和指针相关,unsafer.pointer主要的功能就是不同类型指针间的转换。funcmain(){vara*int8var......
  • vue页面加载闪烁的问题以及解决方案
    一、原因:问题:当我们打开Vue页面的时候,如果弱网环境,会出现一个闪烁的效果下图:加载闪烁问题效果  原因:因为在浏览器中先执行html代码,先渲染Dom,然后再执行JavaScript代......
  • Is L2 Physics-Informed Loss Always Suitable for Training Physics-Informed Neural
      NeurIPS2022本篇工作对PINN中的物理损失进行了探究,作者认为L2损失并不总是适用于训练PINN。并从PDE解的稳定性角度给出了理论性的说明。读了这篇文章,感觉自己的毕......
  • UI通过元素定位实现特定区域截图
    最近计划做一个自动截图的工具,目的是实现性能测试资源监控平台(grafana)各硬件资源的自动截图,解放手工操作。前期的截图做了如下探索。1.整个页面截图1.1代码实现......