Add My second File In QStackwidget.
After pressing the “close” Button from the second file, I can’t able to reopen it. How to reopen the second file? How to refresh the code?
How to close the second/Subwindow file properly and re-open the same from the first file. or If we close the second file, How to refresh the first file fully.
First File
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_())
SecondFile
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_())
Advertisement
Answer
If you have hidden it then you have to call show():
def countrypage(self): self.countrywindow.show() self.stackitems.setCurrentWidget(self.countrywindow)
Note: It is advisable to avoid the abuse of lambda methods, if they are not strictly necessary then do not use them, in your case using a lambda to invoke the close method is unnecessary so you should use:
self.btn1.clicked.connect(self.close)
In addition, the concept of files only serves to order the project but it does not make sense in the execution of the program since the same logic applies if all the logic is in the same file.