Introduction to PyQt5
PyQt5 is one of the most popular Python libraries for creating cross-platform GUI (Graphical User Interface) applications. It combines the power of Python and Qt, a robust C++ framework, enabling developers to create highly responsive and visually appealing desktop applications. With dozens of APIs and tools, PyQt5 provides complete control over GUI elements, events, and advanced features such as multimedia, threading, and database integration.
Key Features and APIs in PyQt5
1. QApplication
The QApplication class is essential for every PyQt5 application. It initializes the application and handles event loops.
from PyQt5.QtWidgets import QApplication import sys app = QApplication(sys.argv) print("PyQt5 application is running!") sys.exit(app.exec_())
2. QMainWindow
This class provides the main application window, including support for menus, toolbars, and a status bar.
from PyQt5.QtWidgets import QApplication, QMainWindow import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('PyQt5 Main Window') self.setGeometry(100, 100, 800, 600) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
3. QWidget
The QWidget is a generic container for GUI controls and serves as a base class for other UI components.
from PyQt5.QtWidgets import QApplication, QWidget, QLabel import sys app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('QWidget Example') window.setGeometry(100, 100, 300, 200) label = QLabel('Hello, PyQt5!', window) label.move(50, 50) window.show() sys.exit(app.exec_())
4. QPushButton
The QPushButton is a button element for user interaction. Below is an example demonstrating its usage:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton import sys def on_click(): print('Button clicked!') app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('QPushButton Example') window.setGeometry(100, 100, 300, 200) button = QPushButton('Click Me', window) button.clicked.connect(on_click) button.move(100, 100) window.show() sys.exit(app.exec_())
5. QLineEdit
QLineEdit is a control for single-line text input.
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit import sys app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('QLineEdit Example') window.setGeometry(100, 100, 400, 200) line_edit = QLineEdit(window) line_edit.setPlaceholderText('Enter some text...') line_edit.move(50, 50) window.show() sys.exit(app.exec_())
6. QMessageBox
This offers a pre-configured dialog box used to show messages or obtain user decisions.
from PyQt5.QtWidgets import QApplication, QMessageBox import sys app = QApplication(sys.argv) msg_box = QMessageBox() msg_box.setText('Are you sure you want to continue?') msg_box.setWindowTitle('Confirmation') msg_box.exec_()
7. QFileDialog
The QFileDialog allows users to browse and select files or directories.
from PyQt5.QtWidgets import QApplication, QFileDialog import sys app = QApplication(sys.argv) file_name, _ = QFileDialog.getOpenFileName(None, 'Select File', '', 'All Files (*)') print('Selected File:', file_name)
8. QVBoxLayout and QHBoxLayout
These layout managers help in organizing widgets vertically or horizontally.
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton import sys app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('Layouts in PyQt5') layout = QVBoxLayout() button1 = QPushButton('Button 1') button2 = QPushButton('Button 2') layout.addWidget(button1) layout.addWidget(button2) window.setLayout(layout) window.show() sys.exit(app.exec_())
Complete Example: A Simple To-Do App
Below is an example of a minimal to-do application using some of the APIs discussed above.
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QListWidget import sys def add_task(): task = task_input.text() if task: task_list.addItem(task) task_input.clear() app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('To-Do App') layout = QVBoxLayout() task_input = QLineEdit() task_input.setPlaceholderText('Enter a new task...') add_button = QPushButton('Add Task') add_button.clicked.connect(add_task) task_list = QListWidget() layout.addWidget(task_input) layout.addWidget(add_button) layout.addWidget(task_list) window.setLayout(layout) window.show() sys.exit(app.exec_())
Conclusion
PyQt5 is a powerful tool for building desktop applications, providing a rich set of APIs and tools that simplify application development. By mastering its core components such as QApplication
, QWidget
, and layout managers, you can unleash its full potential and create both simple and complex applications efficiently.