Learn Python PyQt5 in 1 hour! 🐍 (2024)

3 min read 15 days ago
Published on Aug 19, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to introduce you to Python's PyQt5 library for creating Graphical User Interfaces (GUIs) in just one hour. Whether you're a beginner or looking to refresh your skills, this guide will walk you through fundamental concepts and components of PyQt5, helping you build functional interfaces for your applications.

Step 1: Setting Up Your Environment

To get started with PyQt5, you need to set up your development environment.

  • Install Python: Ensure you have Python installed on your machine. You can download it from the official website.
  • Install PyQt5: Open your terminal or command prompt and run the following command:
    pip install PyQt5
    
  • Choose an IDE: Use an Integrated Development Environment (IDE) like PyCharm, VSCode, or any text editor you prefer for writing your code.

Step 2: Creating Your First GUI Window

Start by creating a simple GUI window using PyQt5.

  1. Import the required modules:
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow
    
  2. Set up the main application:
    app = QApplication(sys.argv)
    window = QMainWindow()
    window.setWindowTitle('My First PyQt5 App')
    window.setGeometry(100, 100, 600, 400)  # (x, y, width, height)
    window.show()
    sys.exit(app.exec_())
    
  3. Run your script, and you should see a window appear.

Step 3: Adding Labels to Your GUI

Labels are essential for displaying text in your application.

  1. Import the QLabel class:
    from PyQt5.QtWidgets import QLabel
    
  2. Create a label and add it to your main window:
    label = QLabel('Hello, PyQt5!', window)
    label.move(200, 150)  # Position the label within the window
    

Step 4: Displaying Images

You can enhance your GUI by adding images.

  1. Import the necessary module:
    from PyQt5.QtGui import QPixmap
    
  2. Load and display an image:
    image = QLabel(window)
    pixmap = QPixmap('path/to/image.png')  # Specify the path to your image
    image.setPixmap(pixmap)
    image.move(100, 50)
    

Step 5: Using Layout Managers

Layout managers help organize your widgets neatly.

  • Use QVBoxLayout or QHBoxLayout for vertical or horizontal arrangements.
  • Example of using QVBoxLayout:
    from PyQt5.QtWidgets import QVBoxLayout, QWidget
    
    layout = QVBoxLayout()
    layout.addWidget(label)
    layout.addWidget(image)
    
    central_widget = QWidget()
    central_widget.setLayout(layout)
    window.setCentralWidget(central_widget)
    

Step 6: Adding Push Buttons

Buttons allow user interaction within your GUI.

  1. Import QPushButton:
    from PyQt5.QtWidgets import QPushButton
    
  2. Create a button and connect it to a function:
    def on_click():
        print('Button Clicked!')
    
    button = QPushButton('Click Me', window)
    button.clicked.connect(on_click)
    button.move(250, 200)
    

Step 7: Implementing Checkboxes

Checkboxes let users make selections.

  1. Import QCheckBox:
    from PyQt5.QtWidgets import QCheckBox
    
  2. Create a checkbox:
    checkbox = QCheckBox('Check Me', window)
    checkbox.move(250, 250)
    

Step 8: Adding Radio Buttons

Radio buttons allow for single-choice selections.

  1. Import QRadioButton:
    from PyQt5.QtWidgets import QRadioButton
    
  2. Create radio buttons:
    radio1 = QRadioButton('Option 1', window)
    radio2 = QRadioButton('Option 2', window)
    radio1.move(250, 300)
    radio2.move(250, 320)
    

Step 9: Using Line Edits for User Input

Line edits allow users to enter text.

  1. Import QLineEdit:
    from PyQt5.QtWidgets import QLineEdit
    
  2. Create a line edit:
    line_edit = QLineEdit(window)
    line_edit.move(250, 350)
    

Step 10: Applying CSS Properties

Customize the appearance of your widgets using CSS.

  • Example of styling a button:
    button.setStyleSheet("background-color: blue; color: white; font-size: 16px;")
    

Conclusion

You have now learned the basics of creating a GUI application with PyQt5. Key components include setting up the environment, creating windows, adding various widgets like labels, buttons, checkboxes, radio buttons, and applying styles.

Next steps could involve exploring more advanced topics such as event handling, integrating databases, or creating more complex layouts. Happy coding!