要直接加载UI文件,我们需要QtUiTools 模块中的类:
from PySide2.QtUiTools import QUiLoader
注意设置工作目录,QUiLoader 允许我们动态加载UI文件并立即使用它
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile, QDir
if __name__ == "__main__":
app = QApplication(sys.argv)
QDir.setCurrent("E:/qpython/Using UI Files")
ui_file = QFile("mainwindow.ui")
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
window = loader.load(ui_file)
ui_file.close()
window.show()
sys.exit(app.exec_())