In this PyQt5 article we want to learn How to Open Second Window with PyQt5, so PyQt5 is a set of Python bindings for Qt v5. it is implemented as more than 35 extension modules and enables Python to be used as an alternative application development language to C++ on all supported platforms including iOS and Android. PyQt5 is used to write all kinds of GUI applications, from accounting applications, to visualization tools used by scientists and engineers.
You can install PyQt5 using pip like this.
1 |
pip install PyQt5 |
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 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel,QPushButton, QWidget from PyQt5.QtGui import QFont class Window1(QMainWindow): def __init__(self): super().__init__() # Create a button and connect it to a method self.button = QPushButton("Open Window 2", self) self.button.clicked.connect(self.open_window2) self.setWindowTitle("Geekscoders.com") def open_window2(self): # Create an instance of Window2 and show it self.window2 = Window2() self.window2.show() class Window2(QWidget): def __init__(self): super().__init__() # Set the window properties self.setWindowTitle("Window 2") self.setGeometry(100, 100, 300, 300) label = QLabel("Welcome to geekscoders.com", self) label.setFont(QFont("Times", 18)) if __name__ == '__main__': app = QApplication([]) window1 = Window1() window1.show() app.exec_() |
In the above example we have created two windows, Window1 and Window2. Window1 contains a button that when we click on that, it opens Window2. open_window2 method creates new instance of Window2 and shows it using the show method, also in Window2 we have added QLabel in the window.
Make sure to import both windows in your main file, and create an instance of the first window to show it. after that when you run your program, both windows will be displayed and you can click the button in Window1 to open Window2.
Run the complete code and this will be the result
Learn More on Python GUI
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6