E7 - multiwindow

from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget

import sys

class AnotherWindow(QWidget):

    def __init__(self):
    
        super().__init__()
        self.setWindowTitle("Another Window")
        self.resize(400, 300)
        
        layout = QVBoxLayout()
        self.setLayout(layout)
        self.label = QLabel("This is another window")
        layout.addWidget(self.label)
        
        
class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")
        self.resize(400, 300)
        
        self.button = QPushButton("Open Another Window")
        self.button.clicked.connect(self.open_another_window)
        self.setCentralWidget(self.button)
        
        
    def open_another_window(self):
        another_window = AnotherWindow()
        another_window.show()
        
        
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
    def open_another_window(self):
        self.another_window = AnotherWindow()
        self.another_window.show()
class MainWindow(QMainWindow):

    def __init__(self):
    
        super().__init__()
        self.setWindowTitle("Main Window")
        self.resize(400, 300)
        
        self.another_window = None
        
        self.button = QPushButton("Open Another Window")
        self.button.clicked.connect(self.open_another_window)
        self.setCentralWidget(self.button)
        
    def open_another_window(self):
    
        if self.another_window is None:
            self.another_window = AnotherWindow()
        self.another_window.show()
        

from random import randint


class AnotherWindow(QWidget):
    
    def __init__(self):
        
        super().__init__()
        self.setWindowTitle("Another Window")
        self.resize(400, 300)
        layout = QVBoxLayout()
        self.setLayout(layout)
        self.label = QLabel("This is another window % d" % randint(0, 100))
        layout.addWidget(self.label)
        
        
class MainWindow(QMainWindow):
    
    def __init__(self):
        
        super().__init__()
        self.setWindowTitle("Main Window")
        self.resize(400, 300)
        
        self.another_window = AnotherWindow()

        self.button = QPushButton("Open Another Window")
        self.button.clicked.connect(self.toggle_window)
        self.setCentralWidget(self.button)
        
            
    def toggle_window(self):
            
            if self.another_window.isVisible():
                self.another_window.hide()
            else:
                self.another_window.show()
class MainWindow(QMainWindow):
    
    def __init__(self):
        
        super().__init__()
        self.setWindowTitle("Main Window")
        self.resize(400, 300)
        
        self.another_window1 = AnotherWindow()
        self.another_window2 = AnotherWindow()
        
        layout = QVBoxLayout()
        
        self.button1 = QPushButton("Open Another Window 1")
        self.button1.clicked.connect(self.toggle_window1)
        
        self.button2 = QPushButton("Open Another Window 2")
        self.button2.clicked.connect(self.toggle_window2)
        
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        
        w = QWidget()
        w.setLayout(layout)
        self.setCentralWidget(w)
        
            
    def toggle_window1(self):
            
            if self.another_window1.isVisible():
                self.another_window1.hide()
            else:
                self.another_window1.show()
                
    def toggle_window2(self):
            
            if self.another_window2.isVisible():
                self.another_window2.hide()
            else:
                self.another_window2.show()