1 法一:pyuic把ui编绎为py文件后import
class MainWnd(QMainWindow):
def __init__(self):
super().__init__()
self.ui = MainUI.Ui_MainUI()
self.ui.setupUi(self)
if __name__ == '__main__':
app = QApplication()
gv.mainWnd = MainWnd()
gv.mainWnd.show()
sys.exit(app.exec_())
优点: 有智能补全, 建议控件较多的界面用此方法
缺点: 写法较复杂
2 法二:通过QUiLoader()动态加载ui文件
from PySide2.QtUiTools import QUiLoader
class MainWnd():
def __init__(self):
self.ui = QUiLoader().load("ui/MainUI.ui")
if __name__ == '__main__':
app = QApplication()
gv.mainWnd = MainWnd()
gv.mainWnd.ui.show()
sys.exit(app.exec_())
优点: 写法简洁
缺点: 没有智能补全, 程序打包后无法加载页面, 因为.ui文件被内嵌入exe了
3 法三: 多继承(推荐)
from ui.MainUI import Ui_MainUI
class MainWnd(QMainWindow, Ui_MainUI):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == '__main__':
app = QApplication()
gv.mainWnd = MainWnd()
gv.mainWnd.show()
sys.exit(app.exec_())
优点: 写法简洁
缺点: 暂无