0

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

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
S Andrew
  • 5,592
  • 27
  • 115
  • 237

1 Answers1

0

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_())
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    Sorry but I didn't get you. Can you please explain a bit more – S Andrew Jul 29 '20 at 10:41
  • I didnt get your code. Is this code will come under button click event `show_registration_window` . ? – S Andrew Jul 29 '20 at 13:18
  • you need 1 function to init the app and that should be neither in the Register nor in the login class... because those are actually "Views" and you are missing a controller – ΦXocę 웃 Пepeúpa ツ Jul 29 '20 at 13:51
  • So where should I put your code, in a separate file.? – S Andrew Jul 29 '20 at 13:54
  • that would be a good idea... but you can use the register file too, just paste my code in your register.py and remove the app from the login – ΦXocę 웃 Пepeúpa ツ Jul 29 '20 at 14:04
  • Apologies. I am not able to fully understand your answer. Unfortunately we do not have any further options to connect and resolve. Thanks for your suggestions, I will try to resolve it. – S Andrew Jul 29 '20 at 14:33
  • Thanks for that. Now I got it where the confusion is. I am not doing any login task. In my login window there is a button to open registration window. If a user click that button, registration window should open (which I am able to do) and all the functionality of the registration window like clicking button should happen (which I am not able to do). – S Andrew Jul 29 '20 at 15:26
  • Sorry if I created any confusion in the question and wasted your time. – S Andrew Jul 29 '20 at 15:27
  • then connect the button click with the registration exec :) – ΦXocę 웃 Пepeúpa ツ Jul 29 '20 at 15:30