使用pyqt5和qt designer

时间:2016-12-14 19:30:03

标签: qt pyqt5 qdialog childwindow

我正在使用pyqt5和QtDesigner创建一个带有两个窗口的应用程序。主窗口“MainWindow.ui”上的一个按钮应该从文件“age_entry.ui”打开第二个窗口,但我似乎遗漏了一些东西。单击第一个表单上的按钮会产生此错误“:

  

异常“未处理的TypeError”   QDialog(parent:QWidget = None,flags:Union [Qt.WindowFlags,Qt.WindowType] = Qt.WindowFlags()):参数1有意外类型'bool'

以下是主窗口的代码:

# -*- 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_())

提前致谢!

以下是代码格式的MainWindow和age_entry文件:

# -*- 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"))

和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"))

1 个答案:

答案 0 :(得分:1)

你的缩进错误有两个问题。其次是你失踪的自我。在showAgeEntryForm上。试试这个:

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()