PyQt: how to load multiple .ui Files from Qt Designer

前端 未结 1 703
礼貌的吻别
礼貌的吻别 2021-01-03 02:33

I want to add startup window that when I click button, it will open another window and close current window. For each window, it has seperated UI which created from Qt Desig

相关标签:
1条回答
  • 2021-01-03 02:46

    First you have an error, you must change __int__ to __init__. To close the window call the close() method.

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtGui import QIcon
    from PyQt5 import uic
    
    #load both ui file
    uifile_1 = 'UI/openPage.ui'
    form_1, base_1 = uic.loadUiType(uifile_1)
    
    uifile_2 = 'UI/mainPage.ui'
    form_2, base_2 = uic.loadUiType(uifile_2)
    
    class Example(base_1, form_1):
        def __init__(self):
            super(base_1,self).__init__()
            self.setupUi(self)
            self.startButton.clicked.connect(self.change)
    
        def change(self):
            self.main = MainPage()
            self.main.show()
            self.close()
    
    class MainPage(base_2, form_2):
        def __init__(self):
            super(base_2, self).__init__()
            self.setupUi(self)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = Example()
        ex.show()
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题