-1

I am making a pyqt program to register and authorize a user. I want to have a start window with buttons that, when clicked, will open either an authorization window or a registration window. But when I import the registration module, for example, the registration window opens instead of the main window. Can you please tell me where I made a mistake?

Main module:

from PyQt5 import QtWidgets, QtCore
from start import Ui_Form #py file with start window ui properties 
import registration #registration module
class StartWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(StartWindow, self).__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.RegButton.clicked.connect(reg_clicked())
        self.openreg=registration.mywindow(self)
        #self.ui.AuthButton.clicked.connect()

    def reg_clicked(self):
        self.openreg.show()
        
app = QtWidgets.QApplication([])
application = StartWindow()
application.show()
sys.exit(app.exec())

Part of registration module:

from PyQt5 import QtWidgets, QtCore
import record_proc
from reg import Ui_Reg

class mywindow(QtWidgets.QMainWindow):
    number_of_samples=1
    def __init__(self):
        super(mywindow, self).__init__()
        self.ui = Ui_Reg()
        self.ui.setupUi(self)
        self.ui.RecordButton.clicked.connect(self.Recording)
        self.ui.AuthButton.clicked.connect(self.Register)
        self.ui.CheckLogin.clicked.connect(self.CheckLogin)
app2 = QtWidgets.QApplication([])
application2 = mywindow()
application2.show()
sys.exit(app2.exec())
alex
  • 1
  • 1
  • 2
    remove the last four lines from `registration.py` or put them in a `if __name__ == "__main__":` block. If you import a module, all of the corresponding .py file is read and executed. In this case, when you import `registration`, the last four lines in your `registration.py` file will create a `QApplication` instance, create and show a registration window and start the event loop. – Heike Apr 12 '21 at 14:02

1 Answers1

0

Your mistake is that you mix executable code and library code.

When you do import registration in your main module, that will execute the registration.py file. That means it will run every line of code in that file, including the application2.show() and sys.exit(). This means the import registration step will never finish, because the act of importing it runs application2.show(), which blocks until you close the registration window. Then, it will run sys.exit(), quitting the program.

I guess what you intended was that the last four lines of the registration module only get executed if you run the script manually, for testing purposes. There is a mechanism built into python that allows exactly that, the if __name__ == "__main__" mechanism.

This should fix your code:

Main module:

from PyQt5 import QtWidgets, QtCore
from start import Ui_Form #py file with start window ui properties 
import registration #registration module
class StartWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(StartWindow, self).__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.RegButton.clicked.connect(reg_clicked())
        self.openreg=registration.mywindow(self)
        #self.ui.AuthButton.clicked.connect()

    def reg_clicked(self):
        self.openreg.show()
        
def main():
    app = QtWidgets.QApplication([])
    application = StartWindow()
    application.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    main()

Registration module:

from PyQt5 import QtWidgets, QtCore
import record_proc
from reg import Ui_Reg

class mywindow(QtWidgets.QMainWindow):
    number_of_samples=1
    def __init__(self):
        super(mywindow, self).__init__()
        self.ui = Ui_Reg()
        self.ui.setupUi(self)
        self.ui.RecordButton.clicked.connect(self.Recording)
        self.ui.AuthButton.clicked.connect(self.Register)
        self.ui.CheckLogin.clicked.connect(self.CheckLogin)

def main():
    app2 = QtWidgets.QApplication([])
    application2 = mywindow()
    application2.show()
    sys.exit(app2.exec())

if __name__ == "__main__":
    main()

The reasoning behind that is that whenever you run a python file directly with python3 file.py, the variable __name__ gets set to "__main__" to signal the file that it now gets executed. So __name__ == "__main__" is not a hack, but an official python mechanism.

For more information, read: https://docs.python.org/3/library/__main__.html

Finomnis
  • 18,094
  • 1
  • 20
  • 27