0

I want to open a form that is represented in a register file that is opened by a click in register button in the login window (main.py) but when the registration form is closed, it can not be opened again in the same session.

But I have to close the whole program and then open it again so that I can get rid of the register only once... and so on.

As well as how to become the register form child of the main model.

If you need to register some users when you open the application then close the form and do some transactions in the same application. And then return to register another person by clicking on the register button to open its from, The problem occurs that the register file is not imported again and therefore do not open the registration form.

So how do I solve this problem so I can open the registration form or any other form I import several times (in the same session) without closing. The program and opens it again.

This file indicated into main.py:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton
from PyQt5.QtGui import QPixmap, QIcon


app = QApplication(sys.argv)
mainRoot = QWidget()
mainRoot.setWindowTitle('Login')
mainRoot.setWindowIcon(QIcon('image\icon2.png'))
lblBackground = QLabel(mainRoot)
pic = QPixmap('image\pic-1.jpg')
lblBackground.setPixmap(pic)
mainRoot.setGeometry(600, 300, 322, 220)


user = QLabel('user name', mainRoot)
user.move(50, 50)
password = QLabel('password', mainRoot)
password.move(50, 80)

username_edt = QLineEdit(mainRoot)
username_edt.move(110, 50)
password_edt = QLineEdit(mainRoot)
password_edt.setEchoMode(QLineEdit.Password)
password_edt.move(110, 75)
signin = QPushButton('sign in', mainRoot)
signin.move(20, 110)
registerBtn = QPushButton('register ', mainRoot)
registerBtn.move(120, 110)
qut_btn = QPushButton('Quit', mainRoot)
qut_btn.move(220, 110)

def registerUser():
    import register

registerBtn.clicked.connect(registerUser)
qut_btn.clicked.connect(quit)

mainRoot.show()
sys.exit(app.exec_())

This file indicated into register.py:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QComboBox, QRadioButton
from PyQt5.QtGui import QPixmap

app = QApplication(sys.argv)
signRoot = QWidget()
signRoot.setWindowTitle('Registration')
lblBackground = QLabel(signRoot)
pix = QPixmap('image\pic-10.jpg')
lblBackground.setPixmap(pix)
signRoot.setGeometry(600, 300, 400, 300)


fullNameLbl = QLabel('Full Name', signRoot)
fullNameLbl.move(100, 50)
userNameLbl = QLabel('User Name', signRoot)
userNameLbl.move(100, 80)
passWord = QLabel('password', signRoot)
passWord.move(100, 110)
typeLbl = QLabel('Type', signRoot)
typeLbl.move(100, 140)
departmentLbl = QLabel('Department', signRoot)
departmentLbl.move(100, 180)


fullName = QLineEdit(signRoot)
fullName.move(200, 50)
userName = QLineEdit(signRoot)
userName.move(200, 80)
passWord = QLineEdit(signRoot)
passWord.setEchoMode(QLineEdit.Password)
passWord.move(200, 110)


maleType = QRadioButton('Male', signRoot)
maleType.move(200, 140)
femaleType = QRadioButton('Female', signRoot)
femaleType.move(270, 140)


department = QComboBox(signRoot)
department.move(200, 180)

submitted = QPushButton('Submit', signRoot)
submitted.move(150, 240)

qut_btn = QPushButton('Quit', signRoot)
qut_btn.move(250, 240)

def qutRegisterForm():
        signRoot.close()




qut_btn.clicked.connect(qutRegisterForm)


signRoot.show()

After run menu form and appear then click on file and register

After register form appear and close it by clicked on exit button

Can't open register form again when I repeat clicked on file and register

  • Thanks for your hint.., I import register form (module_1) as a selection in main program list(main module), and close register form when finished, when I return to open register form again, no response in same session, I can't re-imported again ... why? – Zaher Zaker Apr 07 '19 at 09:21
  • In the modules you should never create windows, remove `win = window()` in register.py – eyllanesc Apr 07 '19 at 17:33
  • Mr. S. Nick solved, Now how can I make registration form as child of the parent login form, I think, You must remove marked as duplicate – Zaher Zaker Apr 07 '19 at 20:58
  • It's still a duplicate, your question of *how to open a new window* has been done a lot of times, or do you think you're the only one who had the problem? IMHO the answer is very poor quality because it only provides a solution without explaining what your solution is based on, if it was a good answer you would have understood: Why in your initial case did not work? Why should you pass a parent? , etc. – eyllanesc Apr 07 '19 at 22:19
  • I'm looking forward to solving the problem of Parent Form and child Form if it is solved ... eyllanesc – Zaher Zaker Apr 08 '19 at 16:06

1 Answers1

0

Try it:

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *

class window(QDialog):           
    def __init__(self):
        super().__init__()
        self.initWin()

    def initWin(self):
        self.gridLayoutCreation()
        vBoxLayout = QVBoxLayout()
        vBoxLayout.addWidget(self.groupBox)
        self.setLayout(vBoxLayout)
# ---       self.show()

    def gridLayoutCreation(self):
        self.groupBox = QGroupBox('')
        gridLayout = QGridLayout()
        qutBtn = QPushButton('Exit')
        gridLayout.addWidget(qutBtn, 0, 0)
        qutBtn.clicked.connect(self.qut)
        self.groupBox.setLayout(gridLayout)

    def qut(self):
        self.close()



class MainWindow(QMainWindow):
    count = 0

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.mdi = QMdiArea()
        self.setCentralWidget(self.mdi)
        bar  = self.menuBar()
        file = bar.addMenu("File")
        file.addAction("New")
        file.addAction("cascade")
        file.addAction("Tiled")
        file.triggered[QAction].connect(self.openRegister)

    def openRegister(self, q):
        print("triggered-> q ->", q.text())
        if q.text() == "New":
            MainWindow.count = MainWindow.count+1
            sub = QMdiSubWindow()
            sub.setAttribute(Qt.WA_DeleteOnClose)
            sub.setWidget(window())                                # <---                                  
            sub.setWindowTitle("subwindow"+str(MainWindow.count))
            sub.setGeometry(100, 100, 400, 400)
            self.mdi.addSubWindow(sub)
            sub.show()

        if q.text() == "cascade":
            self.mdi.cascadeSubWindows()

        if q.text() == "Tiled":
            self.mdi.tileSubWindows()        

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    ex = MainWindow()
    ex.setWindowTitle("Menu")
    ex.show()
    sys.exit(app.exec_())

enter image description here


Update

main.py

import sys

from PyQt5.QtWidgets import QMainWindow, QMdiArea, QAction, QApplication

from register import window                                 # +++

class MainWindow(QMainWindow):
    count = 0

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.mdi = QMdiArea()
        self.setCentralWidget(self.mdi)
        bar = self.menuBar()

        file = bar.addMenu("File")

        open_action = QAction('Open', self)                  # +++
        file.addAction(open_action)                          # +++

        file.triggered[QAction].connect(self.openRegister)
        self.setWindowTitle("Menu")

    def openRegister(self):
#        from register import window
        self.win = window(self.mdi)                           # +++

def main():
    app = QApplication(sys.argv)
    ex = MainWindow()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

register.py

from PyQt5.QtWidgets import  QPushButton, QGridLayout, QDialog,QGroupBox, QVBoxLayout, QApplication


class window(QDialog):
#    def __init__(self):
#        super().__init__()
    def __init__(self, parent=None):
        super(window, self).__init__(parent)

        self.title = 'register'

        self.initWin()

    def initWin(self):
        self.setWindowTitle(self.title)


        self.gridLayoutCreation()
        vBoxLayout = QVBoxLayout()
        vBoxLayout.addWidget(self.groupBox)
        self.setLayout(vBoxLayout)
        self.show()

    def gridLayoutCreation(self):
        self.groupBox = QGroupBox('')
        gridLayout = QGridLayout()

        qutBtn = QPushButton('Exit')

        gridLayout.addWidget(qutBtn, 0, 0)
        qutBtn.clicked.connect(self.qut)


        self.groupBox.setLayout(gridLayout)

    def qut(self):
        self.close()


# win = window()
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    ex = window()
    ex.setWindowTitle("Menu")
    ex.show()
    sys.exit(app.exec_())

enter image description here


Update 2

main.py

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton
from PyQt5.QtGui import QPixmap, QIcon


app = QApplication(sys.argv)
mainRoot = QWidget()
mainRoot.setWindowTitle('Login')
mainRoot.setWindowIcon(QIcon('im.png'))
lblBackground = QLabel(mainRoot)
pic = QPixmap('Ok.png')
lblBackground.setPixmap(pic)
mainRoot.setGeometry(600, 300, 322, 220)

user = QLabel('user name', mainRoot)
user.move(50, 50)
password = QLabel('password', mainRoot)
password.move(50, 80)

username_edt = QLineEdit(mainRoot)
username_edt.move(110, 50)
password_edt = QLineEdit(mainRoot)
password_edt.setEchoMode(QLineEdit.Password)
password_edt.move(110, 75)
signin = QPushButton('sign in', mainRoot)
signin.move(20, 110)
registerBtn = QPushButton('register ', mainRoot)
registerBtn.move(120, 110)
qut_btn = QPushButton('Quit', mainRoot)
qut_btn.move(220, 110)

def registerUser():
#    import register
    from register import registerForm    
    form = registerForm()
    form.show()


registerBtn.clicked.connect(registerUser)
qut_btn.clicked.connect(quit)

mainRoot.show()
sys.exit(app.exec_())

register.py

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QComboBox, QRadioButton
from PyQt5.QtGui import QPixmap

app = QApplication(sys.argv)

def registerForm():                                       # <=======
    signRoot = QWidget()
    signRoot.setWindowTitle('Registration')
    lblBackground = QLabel(signRoot)
    pix = QPixmap('image\pic-10.jpg')
    lblBackground.setPixmap(pix)
    signRoot.setGeometry(600, 300, 400, 300)

    fullNameLbl = QLabel('Full Name', signRoot)
    fullNameLbl.move(100, 50)
    userNameLbl = QLabel('User Name', signRoot)
    userNameLbl.move(100, 80)
    passWord = QLabel('password', signRoot)
    passWord.move(100, 110)
    typeLbl = QLabel('Type', signRoot)
    typeLbl.move(100, 140)
    departmentLbl = QLabel('Department', signRoot)
    departmentLbl.move(100, 180)

    fullName = QLineEdit(signRoot)
    fullName.move(200, 50)
    userName = QLineEdit(signRoot)
    userName.move(200, 80)
    passWord = QLineEdit(signRoot)
    passWord.setEchoMode(QLineEdit.Password)
    passWord.move(200, 110)

    maleType = QRadioButton('Male', signRoot)
    maleType.move(200, 140)
    femaleType = QRadioButton('Female', signRoot)
    femaleType.move(270, 140)
    department = QComboBox(signRoot)
    department.move(200, 180)
    submitted = QPushButton('Submit', signRoot)
    submitted.move(150, 240)
    qut_btn = QPushButton('Quit', signRoot)
    qut_btn.move(250, 240)

    def qutRegisterForm():
            signRoot.close()

    qut_btn.clicked.connect(qutRegisterForm)
#    signRoot.show()

    return signRoot                                           # <=======

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • I do not mean an option in a menu that generates sub menus ... No Generates - I have copied a simple form to illustrate the problem - I want to open a form that is represented in a register file that is opened by a command in the program menu (menu.py) but when the registration form is closed, it can not be opened (the same form) again in the same session But I have to close the whole program and then open it again so that I can get rid of the register only once ... and so on. – Zaher Zaker Apr 07 '19 at 05:09
  • As well as how to become the register form child of the main model – Zaher Zaker Apr 07 '19 at 07:22
  • Oops ... I explained to you your code generates a sub menus but the problem is in importing my register form from module and after closing it I can not import my register form again No other form Please copy the code in two files with the same name shown and try .. Can you open the register form and close it and then reopen it again Thanks... sorry did you understand what i mean??? – Zaher Zaker Apr 07 '19 at 16:54
  • @ZaherZaker I don't know if I understood you correctly but try my update. – S. Nick Apr 07 '19 at 17:30
  • Thank you very much for your effort @S. Nick But I've put the actual code with the problem, so please adjust it to fix the problem and How to make the register form the child of the menu form – Zaher Zaker Apr 07 '19 at 18:17
  • @ZaherZaker see update 2 – S. Nick Apr 07 '19 at 18:51
  • Really thanks a lot @S. Nick, Now how can I make register form as child of the parent login form – Zaher Zaker Apr 07 '19 at 20:31