To achieve this functionality using PySide6, you'll need to set up a main window with two actions. When each action is triggered, it will close the current window and open the specified window. Here's a simple example that demonstrates how to do this using PySide6:
python Copy code
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QAction, QWidget, QVBoxLayout, QLabel, QPushButton
class Window1(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window 1")
self.setGeometry(100, 100, 400, 300)
label = QLabel("This is Window 1", self)
button = QPushButton("Back to Main Window", self)
button.clicked.connect(self.backToMain)
layout = QVBoxLayout()
layout.addWidget(label)
layout.addWidget(button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def backToMain(self):
self.close()
self.main_window = MainWindow()
self.main_window.show()
class Window2(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window 2")
self.setGeometry(100, 100, 400, 300)
label = QLabel("This is Window 2", self)
button = QPushButton("Back to Main Window", self)
button.clicked.connect(self.backToMain)
layout = QVBoxLayout()
layout.addWidget(label)
layout.addWidget(button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def backToMain(self):
self.close()
self.main_window = MainWindow()
self.main_window.show()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Main Window")
self.setGeometry(100, 100, 400, 300)
# Create actions
action1 = QAction("Open Window 1", self)
action2 = QAction("Open Window 2", self)
# Connect actions to methods
action1.triggered.connect(self.openWindow1)
action2.triggered.connect(self.openWindow2)
# Add actions to the menu
menu = self.menuBar().addMenu("Actions")
menu.addAction(action1)
menu.addAction(action2)
def openWindow1(self):
self.close()
self.window1 = Window1()
self.window1.show()
def openWindow2(self):
self.close()
self.window2 = Window2()
self.window2.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
When you run this code, a main window with a menu will appear. You can select an action from the menu to open Window 1 or Window 2.