【问题标题】:PyQt5: TypeError: 'Wrong base class of toplevel widget'PyQt5:TypeError:'顶级小部件的错误基类'
【发布时间】:2022-01-08 11:22:21
【问题描述】:

我使用 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)

当我在PyCharm中调试模式运行代码时,出现如下错误,但代码正常运行时不会出现这种情况

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

【问题讨论】:

  • 在控制台/终端/cmd.exe/powershell 中运行时是否收到错误消息?添加有问题的完整错误消息。
  • @furas 当我只是正常运行代码时,没有错误,但是如果我在 PyCharm 中以调试模式运行代码,我会收到我添加到问题中的类型错误
  • 你有消息'Wrong base class of toplevel widget', (&lt;class 'controllers.GUI.NewClassWindow'&gt;, 'QDialog')) 所以我认为它希望你使用QDialog 创建第二个窗口,但你在class PopupWindowONE(QMainWindow): 中使用QMainWindow
  • @furas 非常感谢您的帮助,将class PopupWindowONE(QMainWindow) 更改为class PopupWindowONE(QDialog) 解决了问题。我相信这是因为我在 QtDesigner 中选择了“带按钮的对话框”模板。如果您想回答,我已更改问题以正确反映问题

标签: python python-3.x pyqt pyqt5


【解决方案1】:

您在消息'Wrong base class of toplevel widget', (&lt;class 'controllers.GUI.NewClassWindow'&gt;, 'QDialog') 中有QDialog,所以我认为它期望QDialog 创建第二个窗口,但您在class PopupWindowONE(QMainWindow): 中使用QMainWindow

换句话说,检查你要启动的 .ui 文件的类类型;如果类是QDialog,那么您的python 类需要接收QDialog

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多