Linked Questions

Popular Questions

How to manage second window from main window in python pyqt5

Asked by At

At first, I would thank you.

I'm working with: Python 3.6 and 3.7 64bit - Win10 64bit - PyQT 5.6.11

I created three windows with qt-designer for PyQT:

qt_main.py
qt_second.py
qt_third.py
qt_4th
qt_5th
etc...

I added two buttons in qt_main for opening 2nd and 3rd windows. When I click on btn_newUser in qt_main, the 2nd window is opened without problem.

But: After showing 2nd window, I want to manage 2nd window from qt_main window. or from itself (if you have solution)

note: Is it possible to manage 2nd window from itself when I opened it from qt_main !??

How I can manage Signals/Slots of 2nd window in qt_main window!??.

qt_main.py:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.NonModal)
        MainWindow.setEnabled(True)
        MainWindow.resize(450, 350)
        <...skipped...>
        <...skipped...>
        self.btn_newUser.setObjectName("btn_newUser")
        self.btn_newUser.clicked.connect(self.window_newUser) ##2nd window.
        <...skipped...>
        <...skipped...>
   def window_newUser(self):
        # 2nd window is opened without problem.
        self.Second_MainWindow = QtWidgets.QMainWindow()
        ui = qt_second.Ui_Second_MainWindow()
        ui.setupUi(self.Second_MainWindow)
        self.Second_MainWindow.show()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

qt_second.py:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Second_MainWindow(object):
    def setupUi(self, Second_MainWindow):
        Second_MainWindow.setObjectName("Second_MainWindow")
        Second_MainWindow.setWindowModality(QtCore.Qt.NonModal)
        <...skipped...>
        <...skipped...>
        self.txt_username.setObjectName("txt_username")
        <...skipped...>
        <...skipped...>
        self.btn_save_info.setObjectName("btn_save_info") ##I want to fetch the content of txt_username then validate it and send it to DB.
        <...skipped...>
        <...skipped...>

# I commented it to show this window from qt_main window.
'''
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    Second_MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Second_MainWindow()
    ui.setupUi(Second_MainWindow)
    Second_MainWindow.show()
    sys.exit(app.exec_())
'''

I tried to write a test method/function for handling Click-Acttion in btn_save_info (second window) and I got error:

Traceback (most recent call last):
File ".\qt_main.py", line 211, in window_newUser 
Second_MainWindow.btn
NameError: name "Second_MainWindow" is not defined
def save_info(self):
    QtWidgets.QMessageBox.information(Second_MainWindow, "Plz", "Hi Test", QtWidgets.QMessageBox.Ok)

I confused and tired. How I can to fix this problem !?? where is the root cause of problem !??.

Thanks in advance.

Related Questions