Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why I cant open second Window ?



  • I want to make application when user can see login window and after succesfull login show him main window. Sometimes user may provide command line args (user+password) and I want to miss login window and show him Main window. I have code but Main window is closing by itself after few seconds. All code below:

    import sys
    from PyQt5.QtWidgets import *
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("This is MAIN window")
            self.show()
    
    
    class SecondWindow(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("This is SECOND window")
            self.show()
    
    
    class LoginWindow(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("This is LOGIN window")
            self.lay_Main = QVBoxLayout()
            self.txt_Login = QLineEdit()
            self.txt_Password = QLineEdit()
            self.btn_LoginButton = QPushButton("Login")
            self.btn_LoginButton.clicked.connect(self.login_me)
            self.lay_Main.addWidget(self.txt_Login)
            self.lay_Main.addWidget(self.txt_Password)
            self.lay_Main.addWidget(self.btn_LoginButton)
            self.setLayout(self.lay_Main)
            self.show()
    
        def login_me(self):
            w1 = MainWindow()
            # w1 = FirstWindow()
            w1.exec_()
    
    
    def main():
        app = QApplication(sys.argv)
        window = LoginWindow()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    MainWindow is a QMainWindow and does not have an exec_ method. You MainWindow lifespan is the login_me function.

    Also you should not call show from within the constructor. It's usually the method / class that manages a widget that decides when it's time to show it.

    Then if you want to manage your login differently with and without parameters, MainWindow should not be created by your LoginWindow. Use it like a dialog and if the result is successful then create your other widget and if not either show again the LoginWindow or stop your application.



  • @SGaist said in Why I cant open second Window ?:

    Then if you want to manage your login differently with and without parameters, MainWindow should not be created by your LoginWindow. Use it like a dialog and if the result is

    I have changed a little my code but still cant open Main window after login :(

    import sys
    from PyQt5.QtWidgets import *
    
    
    global window_m
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setGeometry(200, 200, 600, 200)
            self.setWindowTitle("This is MAIN window")
    
    
    class SecondWindow(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("This is SECOND window")
    
    
    class LoginWindow(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("This is LOGIN window")
            self.lay_Main = QVBoxLayout()
            self.txt_Login = QLineEdit()
            self.txt_Password = QLineEdit()
            self.btn_LoginButton = QPushButton("Login")
            self.btn_LoginButton.clicked.connect(self.login_me)
            self.lay_Main.addWidget(self.txt_Login)
            self.lay_Main.addWidget(self.txt_Password)
            self.lay_Main.addWidget(self.btn_LoginButton)
            self.setLayout(self.lay_Main)
    
        def login_me(self):
            window_m.show()
    
    
    def main():
    
        app = QApplication(sys.argv)
    
        window_m = MainWindow()
    
        # no username nor password from command line (we show login window)
        if len(sys.argv) < 2:
            window_l = LoginWindow()
            window_l.show()
    
        # username and password provided (sys.argv[1] = username, sys.argv[2] = password)
        if len(sys.argv) == 3:
            if sys.argv[1] != '' and sys.argv[2] != '':
                window_m.show()
    
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

  • Lifetime Qt Champion

    If you mean through your dialog then it's normal. You still have your scope issue and beside that you try to use an object created in your main function before it even exists.


Log in to reply