简体   繁体   English

PyQt5:TypeError:'顶级小部件的基类错误'

[英]PyQt5: TypeError: 'Wrong base class of toplevel widget'

I have created to .ui files using QtDesigner and I load them into two seperate windows as show below我已经使用 QtDesigner 创建了 .ui 文件,并将它们加载到两个单独的窗口中,如下所示

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()

        # Set up the user interface from Designer.
        uic.loadUi("interface/UI/main.ui", self)

        # Connect up the buttons
        self.button_classes.clicked.connect(self.open_classes)

        self.w = []

    def open_classes(self):
        self.w.append(PopupWindow(self))
        self.w[-1].show()


class PopupWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()

        # Set up the user interface from Designer.
        uic.loadUi("interface/UI/newclass.ui", self)

When I run the code in PyCharm in debug mode, the following error occurs, however this does not happen when the code is run normally当我在调试模式下在 PyCharm 中运行代码时,出现以下错误,但是正常运行代码时不会发生这种情况

TypeError: ('Wrong base class of toplevel widget', (<class 'controllers.GUI.PopupWindow'>, 'QDialog'))

You have QDialog in message 'Wrong base class of toplevel widget', (<class 'controllers.GUI.NewClassWindow'>, 'QDialog') so I think it expects QDialog to create second window but you use QMainWindow in class PopupWindowONE(QMainWindow):你必须QDialog的消息'Wrong base class of toplevel widget', (<class 'controllers.GUI.NewClassWindow'>, 'QDialog')所以我觉得它预计QDialog创建第二个窗口,但您使用QMainWindow中的class PopupWindowONE(QMainWindow):

In other words, check the class type of the .ui file you are going to initiate;换句话说,检查您要启动的 .ui 文件的类类型; if the class is a QDialog then your python class needs to receive a QDialog .如果该类是QDialog那么你的 python 类需要接收一个QDialog

我在使用QDialog时遇到了类似的问题,但将其更改为QMainWindow并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM