从第二个窗口返回主窗口

我有一个主窗口,如果线程工作人员的条件为false,则启动一个线程,它将打开第二个窗口并隐藏该主窗口 我想再次返回到隐藏的主窗口。

我试图从第二个窗口发送信号


class SecondWindow(qtw.QWidget):

    second_window_signal = qtc.pyqtSignal()

     def __init__(self):
        super().__init__()

      self.back_pushbutton.clicked.connect(self.showmainwindow)

    def showmainwindow(self):
        self.second_window_signal.emit()
        self.hide()

So far I was unable to find a way to receive the signal in the main window to reopen it. I have stored the secondwindow object in the main window

   self.windows = []

def hidewindow(self, condition):
        if condition == True:
            self.show()
        else:
            print("false")

            second_window = SecondWindow()
            self.windows.append(second_window)
            second_window.show()
            print(self.windows)

            self.hide()

我需要以某种方式访问​​存储的第二个窗口对象并执行此任务

secondwindow.second_window_signal.connect(self.show)

I asked the question before but it got downvoted and flagged as duplicate. However, the answer did not help me to solve the problem, so I am asking again.


import os
import sys
import random

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg




class SecondWindow(qtw.QWidget):

    second_window_signal = qtc.pyqtSignal()

    def __init__(self):
        super().__init__()



        # def setParent(self, parent: 'QWidget') -> None:
        #     return Parent

        # interface

        # position
        qtRectangle = self.frameGeometry()
        centerPoint = qtw.QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())
        # size
        self.resize(700, 410)
        # frame title
        self.setWindowTitle("Lambda")
        # heading
        heading_label = qtw.QLabel("second window")
        heading_label.setAlignment(qtc.Qt.AlignHCenter)
        # groubox for widget positioning
        self.groupbox = qtw.QGroupBox()
        self.groupbox.setAlignment(qtc.Qt.AlignRight)
        # widgets
        self.back_pushbutton = qtw.QPushButton("return to mainwindow")
        no_connection_label = qtw.QLabel("Bool return is False")
        # layout
        vlayout = qtw.QVBoxLayout()
        vlayout.setAlignment(qtc.Qt.AlignHCenter)
        vlayout.addWidget(self.back_pushbutton)
        self.groupbox.setLayout(vlayout)
        # mainlayout
        main_layout = qtw.QFormLayout()
        main_layout.addRow(self.groupbox)
        main_layout.addRow(no_connection_label)

        self.setLayout(main_layout)

        # function
        self.back_pushbutton.clicked.connect(self.showmainwindow)


    def showmainwindow(self):
        self.second_window_signal.emit()
        self.hide()




class Worker(qtc.QObject):
    """


    """


    core_signal = qtc.pyqtSignal(bool)

    @qtc.pyqtSlot()
    def run(self):
        random_number = random.randint(1,3)

        if random_number < 2:
            self.core_signal.emit(True)
        else:
            self.core_signal.emit(False)




class MainWindow(qtw.QWidget):
    """
    Main Window

    """
    signal_main = qtc.pyqtSignal()

    def __init__(self, ):
        super().__init__()
        # your code will go here


        # ___Plain_Interface__________________________________________________________________
        # position
        qtRectangle = self.frameGeometry()
        centerPoint = qtw.QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())
        # size
        self.resize(700, 410)
        # frame title
        self.setWindowTitle("Lambda")

        # heading
        heading_label = qtw.QLabel("Main Window")
        heading_label.setAlignment(qtc.Qt.AlignHCenter)



        # groubox for widget positioning
        self.groupbox = qtw.QGroupBox(self)
        # groupbox.setAlignment(qtc.Qt.AlignHCenter)


        self.connecton_test_button = qtw.QPushButton("Test Connection")
        # layout
        vlayout = qtw.QVBoxLayout()
        vlayout.setAlignment(qtc.Qt.AlignHCenter)


        self.groupbox.setLayout(vlayout)

        # mainlayout
        main_layout = qtw.QFormLayout()
        main_layout.addRow(heading_label)
        main_layout.setVerticalSpacing(40)
        main_layout.addRow(self.groupbox)

        self.setLayout(main_layout)

        # self.show()

        # ___functionality  ___ all_________________________________________________________________________________

        # ___ worker and thread ____ #

        # worker instance
        self.obj = Worker()

        # thread
        self.thread = qtc.QThread()

        # Move the Worker object to the Thread object
        self.obj.moveToThread(self.thread)

        # 4 - Connect Worker Signals to the Thread slots
        # self.obj.core_signal.connect(self.thread.quit)

        # 5 - Connect Thread started signal to Worker operational slot method
        self.thread.started.connect(self.obj.run)

        # 6 - Start the thread
        self.thread.start()



        #  ___________________________________________________________________________________________________


        # https://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt


        self.obj.core_signal.connect(self.hidewindow)




        # secondwindow.second_window_signal.connect(self.show)



        self.windows = []

    def hidewindow(self, condition):
        if condition == True:
            self.show()
        else:
            print("false")

            second_window = SecondWindow()
            self.windows.append(second_window)
            second_window.show()
            print(self.windows)

            self.hide()




if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())