























How to create pyside6 on button click open another window class, when main window closed close other opened windows.
To achieve this in PySide6, you can connect the main window's closeEvent to a method that closes all other opened windows. Here's an example:
Here's a code example to demonstrate this:
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget, QVBoxLayout
import sys
class SecondaryWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Secondary Window")
self.setGeometry(100, 100, 300, 200)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Main Window")
self.setGeometry(100, 100, 400, 300)
self.opened_windows = []
self.button = QPushButton("Open Secondary Window", self)
self.button.clicked.connect(self.open_secondary_window)
layout = QVBoxLayout()
layout.addWidget(self.button)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
def open_secondary_window(self):
new_window = SecondaryWindow()
new_window.show()
self.opened_windows.append(new_window)
def closeEvent(self, event):
# Close all opened windows when the main window is closed
for window in self.opened_windows:
window.close()
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
SecondaryWindow Class: This represents the additional windows that will be opened when the button is clicked.MainWindow Class:open_secondary_window method creates and shows a new SecondaryWindow and keeps track of it in the opened_windows list.closeEvent method is overridden to ensure that all opened windows are closed when the main window is closed.This setup ensures that secondary windows are properly managed and closed along with the main window.
You can change MainWindow(QMainWindow) to MainWindow(QWidget). This makes MainWindow a subclass of QWidget instead of QMainWindow. When doing this, you'll need to make a few adjustments, such as setting the layout directly on the main window widget since QWidget doesn't have the same central widget concept as QMainWindow.
Here the modified codes:
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from PySide6.QtGui import QCloseEvent
from typing import List
import sys
class SecondaryWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Secondary Window")
self.setGeometry(100, 100, 300, 200)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Main Window")
self.setGeometry(100, 100, 400, 300)
self.opened_windows: List[SecondaryWindow] = []
self.button: QPushButton = QPushButton("Open Secondary Window", self)
self.button.clicked.connect(self.open_secondary_window)
layout: QVBoxLayout = QVBoxLayout(self)
layout.addWidget(self.button)
self.setLayout(layout)
def open_secondary_window(self) -> None:
new_window: SecondaryWindow = SecondaryWindow()
new_window.show()
self.opened_windows.append(new_window)
def closeEvent(self, event: QCloseEvent) -> None:
# Close all opened windows when the main window is closed
for window in self.opened_windows:
window.close()
event.accept()
if __name__ == "__main__":
app: QApplication = QApplication(sys.argv)
main_window: MainWindow = MainWindow()
main_window.show()
sys.exit(app.exec())
MainWindow(QMainWindow) to MainWindow(QWidget).QWidget doesn't have a setCentralWidget method, you directly set the layout on the MainWindow widget using self.setLayout(layout).此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。