Python Pyqt
## Python3.x Python PyQt
PyQt is a powerful Python library for creating Graphical User Interfaces (GUI), which can be used as an alternative to Python's built-in Tkinter.
PyQt is a Python binding for the Qt framework, widely used in desktop application development.
Qt is a cross-platform C++ application development framework.
PyQt allows Python developers to create powerful GUI applications using the Qt library.
PyQt has the following main versions:
* PyQt4: Binding based on Qt4
* PyQt5: Binding based on Qt5
* PyQt6: Binding based on Qt6 (latest version)
### Install PyQt
Install PyQt5 using pip:
# Install PyQt5 pip install PyQt5# Install Qt Designer and Other Tools (Optional) pip install PyQt5-tools
* * *
### Create a Simple Window
The following is the most basic PyQt program that creates a blank window:
## Example
from PyQt5.QtWidgets import QApplication, QWidget
# Create Application Instance
app = QApplication([])
# Create Main Window
window = QWidget()
window.setWindowTitle("My First PyQt Program")
window.setGeometry(100,100,400,300)# (x, y, width, height)
# Show Window
window.show()
# Run Application
app.exec_()
!(#)
### Code Analysis
1. **`QApplication`**: Manages the control flow and main settings of the GUI application.
2. **`QWidget`**: The most basic window class, from which all UI components inherit.
3. **`setWindowTitle()`**: Sets the window title.
4. **`setGeometry()`**: Sets the window position and size.
5. **`show()`**: Displays the window.
6. **`app.exec_()`**: Starts the event loop, waiting for user interaction.
A typical PyQt application consists of the following parts:
1. **QApplication object**: Every PyQt application needs a QApplication instance
2. **Windows and controls**: User interface components
3. **Event loop**: Handles user input and system events
4. **Event handlers**: Functions or methods that respond to events
## Example
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
class MainWindow(QMainWindow):
def __init__ (self):
super(). __init__ ()
# Set Window Title and Size
self.setWindowTitle("My First PyQt Application")
self.setGeometry(100,100,400,300)# x, y, width, height
# Create Button
self.button= QPushButton("Click me",self)
self.button.setGeometry(150,150,100,30)
self.button.clicked.connect(self.button_clicked)
def button_clicked(self):
print("Button Clicked!")
if __name__ =="__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
!(#)
* * *
## Common PyQt Components
### Button (QPushButton)
## Example
from PyQt5.QtWidgets import QPushButton
button = QPushButton("Click me", window)
button.move(150,150)# Set Button Position
### Label (QLabel)
## Example
from PyQt5.QtWidgets import QLabel
label = QLabel("Hello PyQt!", window)
label.move(100,100)
### Text Box (QLineEdit)
## Example
from PyQt5.QtWidgets import QLineEdit
textbox = QLineEdit(window)
textbox.move(100,50)
For more common component content, refer to: [
* * *
## Layout Management (QVBoxLayout)
Using layout managers can automatically adjust component positions:
## Example
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QPushButton
layout = QVBoxLayout()
layout.addWidget(QLabel("Username"))
layout.addWidget(QLineEdit())
layout.addWidget(QPushButton("Login"))
window.setLayout(layout)
For more layout management content, refer to: [
* * *
## Signal and Slot Mechanism
PyQt uses **Signal** and **Slot** mechanism to handle events.
PyQt's signal and slot mechanism is the core mechanism for communication between objects.
* Signal: A notification emitted when a specific event occurs
* Slot: A function or method that responds to signals
## Example
from PyQt5.QtWidgets import QPushButton
def on_button_click():
print("Button ClickedοΌ")
button = QPushButton("Click me", window)
button.clicked.connect(on_button_click)# Connect Signals and Slots
### Custom Signals
## Example
from PyQt5.QtCore import pyqtSignal, QObject
class MyEmitter(QObject):
my_signal = pyqtSignal(str)# Define a Signal
emitter = MyEmitter()
emitter.my_signal.connect(lambda x: print(f"Signal Received: {x}"))
emitter.my_signal.emit("Hello")# Trigger Signal
For more layout management content, refer to: [
* * *
## Using Qt Designer
Qt Designer is a visual design tool that allows you to drag and drop components to design the interface:
* Launch Designer (usually under Libsite-packagesqt5_applicationsQtbin in the Python installation directory)
* Design the interface and save it as a .ui file
* Convert the .ui file to Python code:
pyuic5 input.ui -o output.py
Use the generated interface in your code:
## Example
from PyQt5 import uic
# Load UI File
Form, Window = uic.loadUiType("output.ui")
# Using UI
app = QApplication(sys.argv)
window = Window()
form = Form()
form.setupUi(window)
window.show()
sys.exit(app.exec_())
* * *
## Practice: A Simple Notepad Application
### Interface Design
## Example
import sys
from PyQt5.QtWidgets import(QApplication, QMainWindow, QTextEdit,
QAction, QFileDialog, QMessageBox)
class Notepad(QMainWindow):
def __init__ (self):
super(). __init__ ()
self.initUI()
def initUI(self):
self.text_edit= QTextEdit(self)
self.setCentralWidget(self.text_edit)
self.create_actions()
self.create_menus()
self.setWindowTitle('Simple Notepad')
self.setGeometry(100,100,800,600)
YouTip