PyQt关闭子窗口问题,无限循环?

时间:2019-04-04 21:23:03

标签: python pyqt

在编写一些代码时,我偶然发现了一个我无法解决的问题。我已经精简了代码以仅显示问题。我试过使用MainWindow.close(),但它会关闭第一个窗口。 控制台错误是:

self.close()
  [Previous line repeated 993 more times]
RecursionError: maximum recursion depth exceeded
import sys
from PyQt5 import QtWidgets, QtGui, QtCore

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Open new window")
        self.pushButton.clicked.connect(self.open)

    def open(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_SecondWindow()
        self.ui.setupUi(self.window)
        self.window.show()

class Ui_SecondWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Close new window")
        self.pushButton.clicked.connect(self.close)

    def close(self):
        self.close()   

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:0)

在下一部分中指向此错误的错误:

B

自从调用self.close()以来,您正在调用调用返回的self.close()的类的close()方法,并且您看到它会生成无限循环。

另一个错误是PyQt不建议修改pyuic生成的代码,建议是创建另一个从适当的窗口继承的类,并将pyuic生成的类用作接口。

考虑上一部分中的指示,解决方案是:

def close(self):
    self.close()   

答案 1 :(得分:0)

通过:

import sys
from PyQt5 import QtWidgets, QtGui, QtCore

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Open new window")
        self.pushButton.clicked.connect(self.open)

    def open(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_SecondWindow()
        self.ui.setupUi(self.window)
        self.window.show()



class Ui_SecondWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Close new window")
        self.pushButton.clicked.connect(MainWindow.close)     # - self.close

#    def close(self):
#        self.close()   

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

enter image description here