'PyQt5 Getting second screen functions to work

how are you doing? I have been coding for a couple months. I started with Python, now I am trying to learn PyQt5.

I have created two windows using the Designer Tool. I created a subclass for each specific page, so that it wont mess up my software once i make changes in the Designer.

I managed to make my second window open from the first window. It works well. However, nothing works in window 2 when its opened by window 1. However, when i run window 2 by itself, it does not work.

I am sure it is a inheritance problem that I am messing up. It seems my sofware is inheriting the window 2 layout but not its functions.

I have attached the code. Is it possible for someone to help me out? If possible, i would like a brief explanation on how to fix it too. I want to learn so I can teach others as well.

This is my main window:

   from nonregusrscreen1 import NonReg
from Main import Ui_MainWindow     # needs to import from the main file
from PyQt5 import QtCore, QtGui, QtWidgets #importing PyQt functionality

class mainWin(QtWidgets.QMainWindow, Ui_MainWindow): #mainWin is new subclass. QtWidgets.Whatever the window was / you need to get inheritance from Ui_MainWindow, otherwise you wil lahve to use self.ui.pushbutton....
    def __init__(self, *args, **kwargs):   #passing arguments from whatever was called
       super().__init__(*args, **kwargs)
       self.ui = Ui_MainWindow()
       self.setupUi(self)
       self.pushButton_2.clicked.connect(self.openwindow) # you have to include UI, otherwise it will not work.
    
    def openwindow(self):
        self.window = QtWidgets.QDialog()
        self.ui = NonReg()
        self.ui.setupUi(self.window)
        self.window.show()

    
if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    w = mainWin()
    w.show()
    app.exec_()

This is my second window, here you can see the pushbutton_2 function that only works when the file is opened by itself (not opened using the first window)

     from nonregusrscreen import Ui_NonRegUserScreen
from PyQt5 import QtCore, QtGui, QtWidgets


class NonReg(QtWidgets.QDialog, Ui_NonRegUserScreen):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ui = Ui_NonRegUserScreen
        self.setupUi(self)
        self.pushButton_2.clicked.connect(self.closewindow)

    def closewindow(self):
        self.window = exit()


    
if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    w = NonReg()
    w.show()
    app.exec_()  

Thank you, i truly appreciate your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source