首页 > 解决方案 > 如何在 PyQt5 QStackWidget 中重新打开我关闭的子窗口?

问题描述

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

标签: pythonpyqt5

解决方案


如果你隐藏了它,那么你必须调用 show():

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

注意:建议避免滥用 lambda 方法,如果它们不是绝对必要的,则不要使用它们,在您的情况下使用 lambda 调用 close 方法是不必要的,因此您应该使用:

self.btn1.clicked.connect(self.close)

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


推荐阅读