Bigman 212
Bigman 212

Reputation: 65

PyQt5 calling one form from another

How can I call the second form from the first one? For example, I have MainWindow.ui and Auth.ui. How can I show Auth.ui, when button is Clicked on Main Window? I am using QtDesigner and pyuic to create forms.

MainWindow.py

class MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(323, 118)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.pb_request = QtWidgets.QPushButton(self.centralwidget)
    self.pb_request.setObjectName("pb_request")

    MainWindow.setCentralWidget(self.centralwidget)
    self.pb_request.clicked.connect(...???)
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pb_request.setText(_translate("MainWindow", "Request"))
    self.label.setText(_translate("MainWindow", "Enter term you want to know"))

def main():
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = MainWindow()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

Upvotes: 1

Views: 2775

Answers (1)

eyllanesc
eyllanesc

Reputation: 244132

It is advisable not to modify the class generated by Qt Designer, what should be done is to create a class that uses the generated view to implicit the logic.

For example when one generates the view based on the template it generates a code similar to:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        [...]
    def retranslateUi(self, MainWindow):
        [...]

Then you must create the class that implements the logic:

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
         QtWidgets.QMainWindow.__init__(self, parent=parent)
         self.setupUi(self)

If you use a template-based view:

  • Mainwindow: Create a class based on QMainWindow.
  • Dialog with Buttons Botton, Dialog with Buttons Right, Dialog without Buttons: Create a class based on QDialog.
  • Widget: Create a class based on QWidget.

Assuming you have created the classes as I recommend:

class Ui_Auth(object):
    def setupUi(self, Auth):
        [...]
    def retranslateUi(self, Auth):
        [...]

class Auth(QtWidgets.QDialog, Ui_Auth):
    def  __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent=parent)
        self.setupUi(self)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        [...]
    def retranslateUi(self, MainWindow):
        [...]

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
         QtWidgets.QMainWindow.__init__(self, parent=parent)
         self.setupUi(self)
         self.pb_request.clicked.connect(self.launch)

    def launch(self):
         auth = Auth()
         auth.exec_()

Upvotes: 3

Related Questions