【问题标题】:PyQt - system crash when I shift to the third windowPyQt - 当我切换到第三个窗口时系统崩溃
【发布时间】:2018-02-12 03:15:33
【问题描述】:

我是使用 PyQt 的初学者。我想用 PyQt 构建一个简单的系统,单击按钮时可以切换到不同的窗口。但是切换到第三个窗口时系统总是崩溃。并像这样返回错误

****进程完成,退出代码为 -1073740791 (0xC0000409)*****

我曾尝试改变不同窗口的类型,使其与之前的窗口不同(QDialog→QWidget QWidget→QDialog),但这种方法没有意义。

希望有人能帮我解决这个问题。

这是一个示例代码

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class FirstWindow(QWidget):

    close_signal = pyqtSignal()
    def __init__(self, parent=None):
        super(FirstWindow, self).__init__(parent)
        self.resize(100, 100)
        self.btn = QToolButton(self)
        self.btn.setText("click")

    def closeEvent(self, event):
        self.close_signal.emit()
        self.close()


class SecondWindow(QDialog):
    def __init__(self, parent=None):
        super(SecondWindow, self).__init__(parent)
        self.resize(150, 150)
        self.btn2 = QToolButton(self)
        self.btn2.setText("click")

class ThirdWindow(QDialog):
    def __init__(self, parent=None):
        super(SecondWindow, self).__init__(parent)
        self.resize(200, 200)
        self.setStyleSheet("background: red")

if __name__ == "__main__":
    App = QApplication(sys.argv)
    ex = FirstWindow()
    s = SecondWindow()

    ex.show()
    ex.btn.clicked.connect(s.show)
    s.btn2.clicked.connect(ThirdWindow.show)
    sys.exit(App.exec_())

【问题讨论】:

  • 问题是因为你要打开一个你没有创建的窗口,ThirdWindow是一个类,一个抽象,你要做的就是创建一个类似于你用FirstWindow所做的对象和 SecondWindow 类:ex = FirstWindow ()      s = SecondWindow () 在您的情况下可能是:t = ThirdWindow (),
  • [cont] 另一个错误是ThirdWindow类错误地调用了父类的构造函数class ThirdWindow (QDialog):      def __init __(self, parent = None):          super (**SecondWindow**, self).__init__(parent),应该是:class ThirdWindow (QDialog):      def __init __(self, parent = None):          super (ThirdWindow, self).__init__(parent)
  • 我建议在 cmd 中运行它,因为你会得到一个明确的错误信息:TypeError: show (self): first argument of unbound method must have type 'QWidget' Aborted (core dumped)

标签: python pyqt pyqt5


【解决方案1】:

您没有创建第三类的实例,请使用此调整:

App = QApplication(sys.argv)
first = FirstWindow()
second = SecondWindow()
third = ThirdWindow() # this line was added

first.show()
first.btn.clicked.connect(second.show)
second.btn2.clicked.connect(third.show)
sys.exit(App.exec_())

顺便说一句:你的handle_close 永远不会被调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2014-05-14
    • 2015-09-28
    • 2018-08-28
    • 2019-02-11
    相关资源
    最近更新 更多