如何在python pyqt5中从主窗口管理第二个窗口

时间:2019-02-03 10:18:44

标签: python python-3.x pyqt pyqt5 qt-designer

首先,我要谢谢你。

我正在使用:Python 3.6和3.7 64bit-Win10 64bit-PyQT 5.6.11

我用qt-designer为PyQT创建了三个窗口:

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

我在qt_main中添加了两个按钮,用于打开第二个和第三个窗口。 当我单击btn_newUser中的qt_main时,第二个窗口打开而没有问题。

但是:显示第二个窗口后,我想从qt_main窗口管理第二个窗口。或本身(如果您有解决方案)

注意:当我从qt_main打开第二个窗口时,是否可以从自身管理第二个窗口!

我如何在qt_main窗口中管理第二个窗口的信号/插槽!?

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_())
'''

我试图在btn_save_info(第二个窗口)中编写一种用于处理点击动作的测试方法/函数,但出现错误:

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)

我感到困惑和疲倦。我该如何解决这个问题!问题的根本原因在哪里!

谢谢。

1 个答案:

答案 0 :(得分:0)

问候!

我发现可以通过以下在main.py(1st window)中声明的方法/函数从第一个窗口(main.py)管理第二个窗口:

    def window_newUser(self):
        self.Second_MainWindow = QtWidgets.QMainWindow()
        self.ui2 = qt_second.Ui_Second_MainWindow()
        self.ui2.setupUi(self.Second_MainWindow)
        self.Second_MainWindow.show()
        # I added below line to 2nd-windows-manager in main.py:
        self.ui2.btn_save_info.clicked.connect(self.func_save_info)
        # I wrote below func in main.py. not in 2nd window.
        # and when I clicked on BTN, I got pop message.
    def func_save_info(self):
        QtWidgets.QMessageBox.information(self.Second_MainWindow, "لاع", "لط و رمزقت وامیید.", QtWidgets.QMessageBox.Ok)

谢谢