Home > Mobile >  How to call a second window in the same class?
How to call a second window in the same class?

Time:06-18

how can I call one of two windows that are both in the same class. The bottom line is this: first a Dialog window opens, then the login_clicked function works(it was not added to the repl code), and when you click on the button in the Dialog window, the login_clicked() function is triggered,and at the end of the login_clicked() function, the SecondWin window already opens, and the Dialog window is destroyed with .destroy()

P.S if you advise creating 2 separate classes, then I also tried, but I can't link them and call the SecondWin window, I'll be glad if you tell me.

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QApplication
import sys





class Ui_Dialog(object):

def main(self, SecondWin):
    SecondWin.setObjectName("SecWin")
    SecondWin.setFixedSize(530, 416)
    SecondWin.setWindowTitle("HMessage")


    app = QtWidgets.QApplication(sys.argv)
    SecondWin = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.main(SecondWin)
    SecondWin.show()
    app.exec()


def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.setFixedSize(400, 265)
    QtCore.QMetaObject.connectSlotsByName(Dialog)


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
app.exec_()

CodePudding user response:

I have a similar situation with something I use at work and I just use .exec_() on the first login window and when that window is closed, I return whether the login was successful or not. Then I use .show() on the main window if the login worked.

if __name__ == "__main__":
    app            = QtWidgets.QApplication(sys.argv)
    login_window   = LoginWindow()
    login_complete = login_window.exec_()

    if login_complete:                             
        window = MainWindow()   
        window.show()
        sys.exit(app.exec_())

In this example both my windows are in separate classes and the first window will hold the main from executing until that login window is closed. Then it will either show the main window or it will just end the program based on what variable I returned in my LoginWindow() 's self.closeEvent() method.

You really should be making classes for the windows individually because it makes things much easier to keep track of and the scope of each window is easier to follow that way.

Here's an example of initializing a QDialog within the class.

class LoginWindow(QtWidgets.QDialog):
    def __init__(self):
        QtWidgets.QDialog.__init__(self)                                                        
        self.setupUi(self)
        ....
  • Related