In this PyQt6 tutorial we want to learn How to Open Second Window in PyQt6, so PyQt6 is popular Python binding for Qt toolkit. Using that you can create graphical user interfaces for their Python applications.
So first of all you need to install PyQt6 and you can use pip for that.
1 |
pip install PyQt6 |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel from PyQt6.QtGui import QFont, QIcon # Main application window class class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Main Window") self.setWindowIcon(QIcon("codeloop.png")) # Initialize second window to None self.second_window = None # Create button to open the second window button = QPushButton("Open Second Window") # Connect button click to open_second_window method button.clicked.connect(self.open_second_window) # Set the layout of the main window layout = QVBoxLayout() layout.addWidget(button) self.setLayout(layout) def open_second_window(self): # Create and show the second window if it doesn't exist if self.second_window is None: self.second_window = SecondWindow() self.second_window.show() # Second window class class SecondWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Second Window") self.setWindowIcon(QIcon("codeloop.png")) # Create a label with text, font, and color label = QLabel("Second Window - This is codeloop.org") label.setFont(QFont("Times", 15)) label.setStyleSheet('color:green') # Set the layout of the second window layout = QVBoxLayout() layout.addWidget(label) self.setLayout(layout) # Main entry point of the application if __name__ == "__main__": # Create the application instance app = QApplication([]) # Create the main window instance window = MainWindow() # Show the main window window.show() # Start the application event loop app.exec() |
In the above code we have create a second_window attribute on the MainWindow class and initialize it to None. when open_second_window method is called, it checks if the second_window attribute is None. if it is None, it creates a new SecondWindow object and assigns it to the second_window attribute. if the second_window attribute is not None, it simply shows the existing SecondWindow object.
this should keep the SecondWindow object alive even after the open_second_window method finishes executing, and it allows the window to remain open.
Run the complete code and this will be the result
FAQs:
How do I open multiple windows in PyQt5?
For opening multiple windows in PyQt5, you need to create instances of QWidget or any of its subclasses (such as QMainWindow), and after that show them. You can define a new class for each window and handle the opening of the new window, typically in response to some user action like a button click.
How do you split a window in PyQt5?
For splitting a window in PyQt5, you can use QSplitter. A QSplitter is a widget that contains other widgets and provides draggable dividers to adjust their sizes.
How to make a new window in Python?
For making a new window in Python using PyQt5, you need to create a class that inherits from QWidget or any of its subclasses, and after that instantiate and show it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from PyQt5.QtWidgets import QApplication, QWidget class WindowExample(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Simple Window") self.setGeometry(100, 100, 400, 300) # Set position and size if __name__ == "__main__": app = QApplication([]) window = WindowExample() window.show() app.exec_() |
Subscribe and Get Free Video Courses & Articles in your Email