首页 > 其他分享 >PyQT调用ui界面文件

PyQT调用ui界面文件

时间:2023-02-04 11:02:13浏览次数:44  
标签:__ 界面 self PyQT ui fname import open


通过QtDesigner将ui文件转存为py文件

  1. 不继承Ui_Form
import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *
from ui_test import Ui_Form

class readfile(QWidget):
    def __init__(self):
        super().__init__()  
        self.ui = Ui_Form()         
        self.ui.setupUi(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__':
	app = QApplication(sys.argv)
	w = readfile()
	w.show()       
	sys.exit(app.exec())
  1. 继承Ui_Form
import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *
from ui_test import Ui_Form

class readfile(Ui_Form,QWidget):
    def __init__(self):
        super().__init__()          
        self.setupUi(self) 
        self.__create_connections()

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


    def open_file(self):        
        fname,_ = QFileDialog.getOpenFileName(self,"打开文件", '.')
        if fname:            
            self.lineEdit.setText(fname)            
            with open(fname,'r') as f:
            	content = f.read()
            self.textBrowser.setText(str(content))                 


if __name__ == '__main__':
	app = QApplication(sys.argv)
	w = readfile()
	w.show()       
	sys.exit(app.exec())

标签:__,界面,self,PyQT,ui,fname,import,open
From: https://www.cnblogs.com/conpi/p/17091076.html

相关文章