Open second window from main with pyqt5 and qt designer

jshort picture jshort · Dec 14, 2016 · Viewed 11k times · Source

I'm creating an application with two windows using pyqt5 and QtDesigner. A button on the main window "MainWindow.ui" should open a second window from the file "age_entry.ui" but I seem to be missing something. Clicking the button on the first form produces this error":

Exception "unhandled TypeError" QDialog(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'bool'

Here is the code for the main window:

# -*- coding: utf-8 -*-

import sys
from PyQt5 import uic, QtWidgets


Ui_MainWindow, QtBaseClass = uic.loadUiType("MainWindow.ui")
LandingPageUI, LandingPageBase = uic.loadUiType("age_entry.ui")

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        def showAgeEntryForm(self):
            self.child_win = AgeEntryForm(self)
            self.child_win.show()             

        self.btnOpenAges.clicked.connect(showAgeEntryForm)            


class AgeEntryForm(LandingPageBase, LandingPageUI):
    def __init__(self, parent=None):   
        LandingPageBase.__init__(self, parent)
        self.setupUi(self)            



if __name__ == "__main__":
    app=QtWidgets.QApplication.instance()
    if not app: 
         app = QtWidgets.QApplication(sys.argv)

    window = MyApp()
    window.show()
    sys.exit(app.exec_())

Thanks in advance!

Here is the MainWindow and age_entry file in code form:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(526, 338)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label_9 = QtWidgets.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(90, 10, 341, 41))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.label_9.setFont(font)
        self.label_9.setObjectName("label_9")
        self.btnOpenAges = QtWidgets.QPushButton(self.centralwidget)
        self.btnOpenAges.setGeometry(QtCore.QRect(130, 90, 181, 61))
        self.btnOpenAges.setObjectName("btnOpenAges")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 526, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label_9.setText(_translate("MainWindow", "Ageing Session Management"))
        self.btnOpenAges.setText(_translate("MainWindow", "Enter Ages"))

And the age_entry.ui

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'age_entry.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(350, 189)
        self.label_13 = QtWidgets.QLabel(Dialog)
        self.label_13.setGeometry(QtCore.QRect(90, 20, 451, 61))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.label_13.setFont(font)
        self.label_13.setObjectName("label_13")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label_13.setText(_translate("Dialog", "Age Entry Form"))

Answer

Dan-Dev picture Dan-Dev · Dec 15, 2016

Two issues your indentation is wrong. Secondly your missing self. on showAgeEntryForm. Try this:

def __init__(self):
    QtWidgets.QMainWindow.__init__(self)
    Ui_MainWindow.__init__(self)
    self.setupUi(self)
    self.btnOpenAges.clicked.connect(self.showAgeEntryForm)

def showAgeEntryForm(self):
    self.child_win = AgeEntryForm(self)
    self.child_win.show()