How to jump from 1 qt window to other in pyqt5.?

245 Views Asked by At

I am working on pyqt5 project where I have login window and registration window. I have a button on login window which when clicked should open the registration window. I have designed the ui in qt desginer and have converted the .ui files to .py files using pyuic.

So I have two files login_ui.py and register_window_ui.py. To use them, I have also created two separate files i.e. login.py and register.py which contains all the functional code of the ui files.

Below is the files code:

login.py

import sys
import os
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from ui.login_ui import Ui_login_main_window
from ui.register_window_ui import Ui_register_window

curr_path = os.path.dirname(os.path.abspath(__file__))

class Login(QMainWindow, Ui_login_main_window):
    def __init__(self):
        QMainWindow.__init__(self)
        self.login_ui = Ui_login_main_window()
        self.login_ui.setupUi(self)

        self.login_ui.register_settings_btn.clicked.connect(self.show_registration_window)

    def show_registration_window(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_register_window()
        self.ui.setupUi(self.window)
        self.window.show()

app = QApplication(sys.argv)
main_window = Login()
main_window.show()
sys.exit(app.exec_())

register.py

import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow
from ui.register_window_ui import Ui_register_window

curr_path = os.path.dirname(os.path.abspath(__file__))

class Register(QMainWindow, Ui_register_window):
    def __init__(self):
        QMainWindow.__init__(self)
        self.register_win_ui = Ui_register_window()
        self.register_win_ui.setupUi(self)

        self.register_win_ui.register_trial_btn.clicked.connect(self.print_data)

    def print_data(self):
        print("Clicked")

app = QApplication(sys.argv)
main_window = Register()
main_window.show()
sys.exit(app.exec_())

As you can see that in login.py I have created a button click event for register_settings_btn which when clicked will show Ui_register_window() which is register_window. So when I click it, it shows me the window but the window is not functional i.e. there is no button click event happening. For ex in register.py I have created a button click event which is not working.

Can anyone please explain me why is it not working. Please help. Thanks

1

There are 1 best solutions below

9
On

I have a button on login window which when clicked should open the registration window

instead of creating 2 QApplications create only 1 and show or exec 1st the login, validate the input and if everything is ok, then show the Register

i.e. instead do something like:

app = QApplication(sys.argv)
login_window = Login()
result = login_window.exec()
if result == ???:
    register_window = Register()
    register_window.show()
else:
    showMsg('Login failed!') 

sys.exit(app.exec_())

update! create another file i.e. myProject.py and add this!

#!/usr/local/bin/python
# coding: utf-8

import os, sys
add your imports here!


if __name__ == "__main__":
    app = QApplication(sys.argv)
    #create login view nd register view
    login_window = Login()
    register_window = Register()
    #execute the view login and read the doc, this will block the code until the user quit the view!
    result = login_window.exec()
    #define a condition to proof the validity of login process
    if result == ???:
        #show the register 
        register_window.show()
    else:
        #or show the error msg
        showMsg('Login failed!') 

    sys.exit(app.exec_())