Python 如何在PyQt5小部件中重新打开我关闭的子窗口?

Python 如何在PyQt5小部件中重新打开我关闭的子窗口?,python,pyqt5,Python,Pyqt5,在QStackwidget中添加我的第二个文件 按下第二个文件的“关闭”按钮后,我无法重新打开它。 如何重新打开第二个文件?如何刷新代码 如何正确关闭第二个/子窗口文件并从第一个文件重新打开该文件。或者,如果关闭第二个文件,如何完全刷新第一个文件 第一个文件 import sys,os from PyQt5.QtWidgets import * from PyQt5.QtCore import * from sample_countrypage import Countrypage class

在QStackwidget中添加我的第二个文件

按下第二个文件的“关闭”按钮后,我无法重新打开它。 如何重新打开第二个文件?如何刷新代码

如何正确关闭第二个/子窗口文件并从第一个文件重新打开该文件。或者,如果关闭第二个文件,如何完全刷新第一个文件

第一个文件

import sys,os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from sample_countrypage import Countrypage

class MainPage(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle(" Sample Programme")
        self.setGeometry(100,100,1000,600)
        self.Ui()
        self.show()

    def Ui(self):
        self.countrywindow = Countrypage()

        self.stackitems = QStackedWidget()

        self.btn1=QPushButton("Open 2nd File")
        self.btn1.setFixedSize(100, 30)
        self.btn1.clicked.connect(self.countrypage)

        self.left_layout = QVBoxLayout()
        self.right_layout = QHBoxLayout()
        self.main_layout = QHBoxLayout()
        self.left_layout.addWidget(self.btn1)
        self.left_layout.addStretch()

        self.right_frame = QFrame()
        self.right_layout = QHBoxLayout(self.right_frame)

        self.main_layout.setSpacing(5)
        self.main_layout.setContentsMargins(0,0,0,0)
        self.main_layout.addLayout(self.left_layout)
        self.main_layout.addWidget(self.right_frame)
        self.main_layout.addStretch()

        self.stackitems.addWidget(self.countrywindow)
        self.right_layout.addWidget(self.stackitems)

        widget = QWidget()
        widget.setLayout(self.main_layout)
        self.setCentralWidget(widget)

    def countrypage(self):
        self.stackitems.setCurrentWidget(self.countrywindow)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = MainPage()
    app.setStyle("fusion")
    mainwindow.show()
    sys.exit(app.exec_())
import sys,os
from PyQt5.QtWidgets import *

class Countrypage(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Country Page")
        self.btn1 = QPushButton("close")
        self.btn1.clicked.connect(lambda : self.close())

        self.form_layout = QFormLayout()
        self.form_layout.addRow("Country",QLineEdit())
        self.form_layout.addRow("continent",QLineEdit())

        self.layout_btn = QHBoxLayout()
        self.layout_btn.addStretch()
        self.layout_btn.addWidget(self.btn1)

        self.layout_country = QVBoxLayout()
        self.layout_country.addLayout(self.form_layout)
        self.layout_country.addLayout(self.layout_btn)
        self.layout_country.addStretch()
        self.setLayout(self.layout_country)
if __name__=="__main__":
    app = QApplication(sys.argv)
    countrywin = Countrypage()
    countrywin.show()
    sys.exit(app.exec_())
第二个文件

import sys,os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from sample_countrypage import Countrypage

class MainPage(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle(" Sample Programme")
        self.setGeometry(100,100,1000,600)
        self.Ui()
        self.show()

    def Ui(self):
        self.countrywindow = Countrypage()

        self.stackitems = QStackedWidget()

        self.btn1=QPushButton("Open 2nd File")
        self.btn1.setFixedSize(100, 30)
        self.btn1.clicked.connect(self.countrypage)

        self.left_layout = QVBoxLayout()
        self.right_layout = QHBoxLayout()
        self.main_layout = QHBoxLayout()
        self.left_layout.addWidget(self.btn1)
        self.left_layout.addStretch()

        self.right_frame = QFrame()
        self.right_layout = QHBoxLayout(self.right_frame)

        self.main_layout.setSpacing(5)
        self.main_layout.setContentsMargins(0,0,0,0)
        self.main_layout.addLayout(self.left_layout)
        self.main_layout.addWidget(self.right_frame)
        self.main_layout.addStretch()

        self.stackitems.addWidget(self.countrywindow)
        self.right_layout.addWidget(self.stackitems)

        widget = QWidget()
        widget.setLayout(self.main_layout)
        self.setCentralWidget(widget)

    def countrypage(self):
        self.stackitems.setCurrentWidget(self.countrywindow)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = MainPage()
    app.setStyle("fusion")
    mainwindow.show()
    sys.exit(app.exec_())
import sys,os
from PyQt5.QtWidgets import *

class Countrypage(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Country Page")
        self.btn1 = QPushButton("close")
        self.btn1.clicked.connect(lambda : self.close())

        self.form_layout = QFormLayout()
        self.form_layout.addRow("Country",QLineEdit())
        self.form_layout.addRow("continent",QLineEdit())

        self.layout_btn = QHBoxLayout()
        self.layout_btn.addStretch()
        self.layout_btn.addWidget(self.btn1)

        self.layout_country = QVBoxLayout()
        self.layout_country.addLayout(self.form_layout)
        self.layout_country.addLayout(self.layout_btn)
        self.layout_country.addStretch()
        self.setLayout(self.layout_country)
if __name__=="__main__":
    app = QApplication(sys.argv)
    countrywin = Countrypage()
    countrywin.show()
    sys.exit(app.exec_())

如果已将其隐藏,则必须调用show():

def countrypage(self):
self.countrywindow.show()
self.stackitems.setCurrentWidget(self.countrywindow)
注意:建议避免滥用lambda方法,如果它们不是严格必需的,则不要使用它们,在您的情况下,使用lambda调用close方法是不必要的,因此您应该使用:

self.btn1.点击.connect(self.close)

此外,文件的概念仅用于订购项目,但在执行程序时没有意义,因为如果所有逻辑都在同一文件中,则应用相同的逻辑。

注意。谢谢注意:在使用术语时要更加小心,“子窗口”是非常不同的东西,它们有非常不同的用途和行为,并且与QStackedWidget(它使用“页面”,即使有时它们被不当地称为“屏幕”)完全无关。