通过QtDesigner将ui文件转存为py文件
- 不继承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())
- 继承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