1 法一:pyuic把ui编绎为py文件后import

  1. class MainWnd(QMainWindow):
  2. def __init__(self):
  3. super().__init__()
  4. self.ui = MainUI.Ui_MainUI()
  5. self.ui.setupUi(self)
  6. if __name__ == '__main__':
  7. app = QApplication()
  8. gv.mainWnd = MainWnd()
  9. gv.mainWnd.show()
  10. sys.exit(app.exec_())

优点: 有智能补全, 建议控件较多的界面用此方法
缺点: 写法较复杂

2 法二:通过QUiLoader()动态加载ui文件

  1. from PySide2.QtUiTools import QUiLoader
  2. class MainWnd():
  3. def __init__(self):
  4. self.ui = QUiLoader().load("ui/MainUI.ui")
  5. if __name__ == '__main__':
  6. app = QApplication()
  7. gv.mainWnd = MainWnd()
  8. gv.mainWnd.ui.show()
  9. sys.exit(app.exec_())

image.png
优点: 写法简洁
缺点: 没有智能补全, 程序打包后无法加载页面, 因为.ui文件被内嵌入exe了

3 法三: 多继承(推荐)

  1. from ui.MainUI import Ui_MainUI
  2. class MainWnd(QMainWindow, Ui_MainUI):
  3. def __init__(self):
  4. super().__init__()
  5. self.setupUi(self)
  6. if __name__ == '__main__':
  7. app = QApplication()
  8. gv.mainWnd = MainWnd()
  9. gv.mainWnd.show()
  10. sys.exit(app.exec_())

优点: 写法简洁
缺点: 暂无