2
votes

I have been trying to develop a multi-window application using the PyQt5 library. I wanted to have a second window dedicated for separate features. I was able to switch from the main window to the secondary window without any problems. However after attempting to switch back, I got the Recursion Error exception. Here is the relevant code:

import os
import sys
from functools import partial
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QFrame, QMessageBox, QMenu         



class IpLogger(QMainWindow):
    def __init__(self):
        super(IpLogger, self).__init__()
        self.setWindowTitle("Main window")
        self.setFixedSize(648, 228)
        self.setWindowIcon(QtGui.QIcon("data\\icon\\WiFicon.ico"))
        self.loggerUI()



    def loggerUI(self):
        #Define font variable
        font = QtGui.QFont()

        #Button leading to second window
        self.mediaBtn = QtWidgets.QPushButton(self)
        self.mediaBtn.setGeometry(QtCore.QRect(10, 180, 181, 41))
        font.setPointSize(10)
        self.mediaBtn.setFont(font)
        self.mediaBtn.setObjectName("mediaBtn")
        self.mediaBtn.setText("Social Media")
        self.mediaBtn.clicked.connect(partial(self.showWindow, Media()))

    #Switching windows
    def showWindow(self, window):
        self.win = window
        self.win.show()
        self.close()



class Media(QMainWindow):
    def __init__(self):
        super(Media, self).__init__()
        self.setWindowTitle("Second window")
        self.setFixedSize(300, 180)
        self.setWindowIcon(QtGui.QIcon("data\\icon\\WiFicon.ico"))
        self.mediaUI()

    def mediaUI(self):
        #Variables
        font = QtGui.QFont()

        #Button leading back to the main menu
        self.backBtn = QtWidgets.QPushButton(self)
        self.backBtn.setGeometry(QtCore.QRect(130, 123, 40, 40))
        font.setFamily("Segoe UI")
        font.setPointSize(10)
        self.backBtn.setFont(font)
        self.backBtn.setObjectName("backBtn")
        self.backBtn.setStyleSheet("background-color: rgb(69, 69, 69, 0)")
        self.backBtn.setIcon(QtGui.QIcon("data\\icon\\back.png"))
        self.backBtn.setIconSize(QtCore.QSize(40, 40))
        self.backBtn.clicked.connect(partial(self.showWindow, IpLogger()))

    #Switching windows
    def showWindow(self, window):
        self.win = window
        self.win.show()
        self.close()



def window():
    app = QApplication(sys.argv)
    win = IpLogger()
    win.show()
    sys.exit(app.exec_())

window()
1
Ok I will do that - Jackal
Should be reproducible now. - Jackal

1 Answers

2
votes

Explanation:

According to your code you create an IpLogger object, this IpLogger object creates in its loggerUI method that it is executed when a Media() object is built, that new object does the same in mediaUI(), that is, creates another IpLogger object, that is, you do what next:

IpLogger->loggerUI->Media->mediaUI->IpLogger->loggerUI->Media->...

Solution:

According to what is explained in the OP post, you only want to have a single IpLogger object and a single Media object, so both objects can be created in the window function and use the connections to exchange the visibility of one or the other depending on the case.

import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class IpLogger(QtWidgets.QMainWindow):
    def __init__(self):
        super(IpLogger, self).__init__()
        self.setWindowTitle("Main window")
        self.setFixedSize(648, 228)
        self.setWindowIcon(QtGui.QIcon("data\\icon\\WiFicon.ico"))
        self.loggerUI()

    def loggerUI(self):
        # Define font variable
        font = QtGui.QFont()

        # Button leading to second window
        self.mediaBtn = QtWidgets.QPushButton(self)
        self.mediaBtn.setGeometry(QtCore.QRect(10, 180, 181, 41))
        font.setPointSize(10)
        self.mediaBtn.setFont(font)
        self.mediaBtn.setObjectName("mediaBtn")
        self.mediaBtn.setText("Social Media")


class Media(QtWidgets.QMainWindow):
    def __init__(self):
        super(Media, self).__init__()
        self.setWindowTitle("Second window")
        self.setFixedSize(300, 180)
        self.setWindowIcon(QtGui.QIcon("data\\icon\\WiFicon.ico"))
        self.mediaUI()

    def mediaUI(self):
        # Variables
        font = QtGui.QFont()

        # Button leading back to the main menu
        self.backBtn = QtWidgets.QPushButton(self)
        self.backBtn.setGeometry(QtCore.QRect(130, 123, 40, 40))
        font.setFamily("Segoe UI")
        font.setPointSize(10)
        self.backBtn.setFont(font)
        self.backBtn.setObjectName("backBtn")
        self.backBtn.setStyleSheet("background-color: rgb(69, 69, 69, 0)")
        self.backBtn.setIcon(QtGui.QIcon("data\\icon\\back.png"))
        self.backBtn.setIconSize(QtCore.QSize(40, 40))


def window():
    app = QtWidgets.QApplication(sys.argv)

    win = IpLogger()
    media = Media()

    win.mediaBtn.clicked.connect(media.show)
    win.mediaBtn.clicked.connect(win.close)

    media.backBtn.clicked.connect(win.show)
    media.backBtn.clicked.connect(media.close)

    win.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    window()