python 多个对话框中超出了最大递归深度

maximum recursion depth exceeded in python multiple dialog

# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore

class Window_Test3(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        global progress_flag
        **self.Next_Window = Window_Test1()   # Return Window_Test1() but RuntimeError: maximum recursion depth exceeded**

  self.setWindowFlags(QtCore.Qt.CustomizeWindowHint|QtCore.Qt.WindowTitleHint|QtCore.Qt.WindowMaximizeButtonHint)
        self.setGeometry(0, 0, 800,480)
        self.setWindowTitle('TEST PROCESSING')

        quit = QtGui.QPushButton('Close', self)

        quit.setGeometry(10, 10, 60, 35)

        test = QtGui.QPushButton('TEST 3', self)
        test.setGeometry(100, 200, 200, 100)


        self.connect(quit, QtCore.SIGNAL('clicked()'),self.reject)
        self.connect(test, QtCore.SIGNAL('clicked()'), self.nextWindow)

    def nextWindow(self):

        self.Next_Window.show()

class Window_Test2(QtGui.QDialog):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        global progress_flag
        self.Window3 = Window_Test3()

        self.setWindowFlags(QtCore.Qt.CustomizeWindowHint|QtCore.Qt.WindowTitleHint|QtCore.Qt.WindowMaximizeButtonHint)
        self.setGeometry(0, 0, 800, 480)
        self.setWindowTitle('TEST PROCESSING')

        self.quit = QtGui.QPushButton('Close', self)
        self.quit.setGeometry(10, 10, 60, 35)

        test = QtGui.QPushButton('TEST 2', self)
        test.setGeometry(250, 220, 200, 100)

        self.connect(self.quit, QtCore.SIGNAL('clicked()'),self.reject)

        self.connect(test, QtCore.SIGNAL('clicked()'),self.nextWindow)

    def nextWindow(self):

        self.Window3.show()

class Window_Test1(QtGui.QDialog):

    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.Window2 = Window_Test2()

        self.setGeometry(0, 0, 800, 480)
        self.setWindowTitle('TEST PROCESSING' )

        test = QtGui.QPushButton('TEST', self)
        test.setGeometry(100,100,100,100)

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(test, QtCore.SIGNAL('clicked()'),self.nextWindow)

        self.connect(quit, QtCore.SIGNAL('clicked()'), self.reject)

    def nextWindow(self):
        self.Window2.show()

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    Window1 = Window_Test1()
    Window1.show()
    sys.exit(app.exec_())

您的代码以递归方式初始化新对象。

Window_Test1调用Window_Test2,调用Window_Test3,调用Window_Test1,调用Window_Test2,调用Window_Test3调用 Window_Test1,这...你明白了。

您可能希望将这些对象的初始化推迟到实际需要时(因此不在 __init__ 中),或者可能需要所有三个 windows 的单个实例提到。

为下一个window创建一个占位符self.next_Window并在创建所有3个windows后填写

# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore

class Window_Test3(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        global progress_flag
        self.next_Window = None

        self.setWindowFlags(QtCore.Qt.CustomizeWindowHint|
                            QtCore.Qt.WindowTitleHint|
                            QtCore.Qt.WindowMaximizeButtonHint)
        self.setGeometry(0, 0, 800,480)
        self.setWindowTitle('TEST PROCESSING')

        quit = QtGui.QPushButton('Close', self)

        quit.setGeometry(10, 10, 60, 35)

        test = QtGui.QPushButton('TEST 3', self)
        test.setGeometry(100, 200, 200, 100)


        self.connect(quit, QtCore.SIGNAL('clicked()'),self.reject)
        self.connect(test, QtCore.SIGNAL('clicked()'), self.nextWindow)

    def nextWindow(self):
        self.next_Window.show()
    def setNextWindow(self, nextWindow):
        self.next_Window = nextWindow

class Window_Test2(QtGui.QDialog):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        global progress_flag
        self.next_Window = None

        self.setWindowFlags(QtCore.Qt.CustomizeWindowHint|
                            QtCore.Qt.WindowTitleHint|
                            QtCore.Qt.WindowMaximizeButtonHint)
        self.setGeometry(0, 0, 800, 480)
        self.setWindowTitle('TEST PROCESSING')

        self.quit = QtGui.QPushButton('Close', self)
        self.quit.setGeometry(10, 10, 60, 35)

        test = QtGui.QPushButton('TEST 2', self)
        test.setGeometry(250, 220, 200, 100)

        self.connect(self.quit, QtCore.SIGNAL('clicked()'),self.reject)

        self.connect(test, QtCore.SIGNAL('clicked()'),self.nextWindow)

    def nextWindow(self):
        self.next_Window.show()
    def setNextWindow(self, nextWindow):
        self.next_Window = nextWindow

class Window_Test1(QtGui.QDialog):

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.next_Window = None

        self.setGeometry(0, 0, 800, 480)
        self.setWindowTitle('TEST PROCESSING' )

        test = QtGui.QPushButton('TEST', self)
        test.setGeometry(100,100,100,100)

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(test, QtCore.SIGNAL('clicked()'),self.nextWindow)
        self.connect(quit, QtCore.SIGNAL('clicked()'), self.reject)

    def nextWindow(self):
        self.next_Window.show()
    def setNextWindow(self, nextWindow):
        self.next_Window = nextWindow

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    Window1 = Window_Test1()
    Window2 = Window_Test2()
    Window3 = Window_Test3()
    Window1.setNextWindow(Window2)
    Window2.setNextWindow(Window3)
    Window3.setNextWindow(Window1)
    Window1.show()
    sys.exit(app.exec_())

应该只有一个 window 而不是三个。

def nextWindow (self):
    self.next_Window.show()
    self.actionEvent (self.reject ()) # Delete current screen

def setNextWindow (self, nextWindow):
    self.next_Window = nextWindow

也就是你加上self.actionEvent(self.reject ()),

一屏输出。

不过切换画面时有轻微抖动