Python | Algorithms | Data Structures | Cyber Security | Networks
Photo
# 📚 PyQt5 Tutorial - Part 2/6: Advanced Widgets & Customization
#PyQt5 #PythonGUI #AdvancedWidgets #QSS #SignalsSlots
Welcome to Part 2 of our PyQt5 series! This in-depth lesson covers advanced widgets, custom styling, multi-window applications, and professional patterns.
---
## 🔹 Advanced Widgets Overview
### 1. Tabbed Interfaces (QTabWidget)
### 2. Tree Widget (QTreeWidget)
### 3. Table Widget (QTableWidget)
---
## 🔹 Custom Signals & Slots
### 1. Creating Custom Signals
### 2. Advanced Signal-Slot Connections
---
## 🔹 Styling with Qt Style Sheets (QSS)
### 1. Basic Styling
### 2. Advanced Selectors
### 3. Dynamic Style Changes
---
#PyQt5 #PythonGUI #AdvancedWidgets #QSS #SignalsSlots
Welcome to Part 2 of our PyQt5 series! This in-depth lesson covers advanced widgets, custom styling, multi-window applications, and professional patterns.
---
## 🔹 Advanced Widgets Overview
### 1. Tabbed Interfaces (QTabWidget)
from PyQt5.QtWidgets import QTabWidget, QTextEdit, QWidget
class TabDemo(QWidget):
def __init__(self):
super().__init__()
tabs = QTabWidget()
# Tab 1: Text Editor
tab1 = QWidget()
text_edit = QTextEdit()
tab1_layout = QVBoxLayout()
tab1_layout.addWidget(text_edit)
tab1.setLayout(tab1_layout)
# Tab 2: Settings
tab2 = QWidget()
tab2_layout = QVBoxLayout()
tab2_layout.addWidget(QLabel("Settings Panel"))
tab2.setLayout(tab2_layout)
tabs.addTab(tab1, "Editor")
tabs.addTab(tab2, "Settings")
main_layout = QVBoxLayout()
main_layout.addWidget(tabs)
self.setLayout(main_layout)
### 2. Tree Widget (QTreeWidget)
def setup_file_tree(self):
tree = QTreeWidget()
tree.setHeaderLabels(["Name", "Size", "Type"])
# Add parent items
root = QTreeWidgetItem(tree)
root.setText(0, "Project Root")
# Add children
for file in ["main.py", "config.ini", "README.md"]:
child = QTreeWidgetItem(root)
child.setText(0, file)
child.setText(1, "10 KB")
child.setText(2, "Python" if file.endswith(".py") else "Text")
tree.expandAll()
return tree
### 3. Table Widget (QTableWidget)
def setup_data_table(self):
table = QTableWidget(5, 3) # Rows, columns
table.setHorizontalHeaderLabels(["ID", "Name", "Status"])
sample_data = [
[101, "Product A", "Active"],
[102, "Product B", "Inactive"],
[103, "Product C", "Pending"]
]
for row, data in enumerate(sample_data):
for col, text in enumerate(data):
item = QTableWidgetItem(str(text))
table.setItem(row, col, item)
table.resizeColumnsToContents()
return table
---
## 🔹 Custom Signals & Slots
### 1. Creating Custom Signals
from PyQt5.QtCore import pyqtSignal, QObject
class Worker(QObject):
progress_changed = pyqtSignal(int)
task_completed = pyqtSignal(str)
def run_task(self):
for i in range(1, 101):
time.sleep(0.05)
self.progress_changed.emit(i)
self.task_completed.emit("Task finished!")
### 2. Advanced Signal-Slot Connections
# Multiple signals to single slot
button1.clicked.connect(self.handle_click)
button2.clicked.connect(self.handle_click)
# Signal with arguments
self.worker.progress_changed.connect(self.update_progress_bar)
# Lambda slots
button.clicked.connect(lambda: self.process_data(param1, param2))
# Slot decorator
@pyqtSlot()
def on_button_click(self):
print("Button clicked!")
---
## 🔹 Styling with Qt Style Sheets (QSS)
### 1. Basic Styling
app.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
border: none;
color: white;
padding: 8px 16px;
font-size: 14px;
}
QPushButton:hover {
background-color: #45a049;
}
QLineEdit {
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
}
""")### 2. Advanced Selectors
/* Style only buttons in the toolbar */
QToolBar QPushButton {
min-width: 80px;
}
/* Style checked checkboxes differently */
QCheckBox:checked {
color: #0085FF;
}
/* Style odd/even table rows */
QTableView::item:alternate {
background: #f0f0f0;
}
### 3. Dynamic Style Changes
# Change style programmatically
button.setStyleSheet("""
QPushButton {
background-color: red;
font-weight: bold;
}
""")
# Reset to default
button.setStyleSheet("")
---
❤1
Python | Algorithms | Data Structures | Cyber Security | Networks
Photo
def show_error(self, message):
self.thread.quit()
QMessageBox.critical(self, "Error", message)
class FileWorker(QObject):
progress = pyqtSignal(int)
finished = pyqtSignal()
error = pyqtSignal(str)
def __init__(self):
super().__init__()
self.files = []
def set_files(self, files):
self.files = files
def process(self):
try:
total = len(self.files)
for i, file in enumerate(self.files):
# Simulate processing
time.sleep(0.5)
# Check for cancellation
if QThread.currentThread().isInterruptionRequested():
break
# Update progress
self.progress.emit(int((i + 1) / total * 100))
self.finished.emit()
except Exception as e:
self.error.emit(str(e))
---
## 🔹 Best Practices
1. Always clean up threads - Use
finished signals2. Never update UI from worker threads - Use signals
3. Validate file operations - Check permissions/existence
4. Handle drag-and-drop properly - Check MIME types
5. Make dialogs modal/non-modal appropriately -
exec_() vs show()---
### 📌 What's Next?
In Part 4, we'll cover:
➡️ Database Integration (SQLite, PostgreSQL)
➡️ Data Visualization (Charts, Graphs)
➡️ Model-View-Controller Pattern
➡️ Advanced Widget Customization
#PyQt5 #ProfessionalDevelopment #PythonGUI 🚀
Practice Exercise:
1. Build a thumbnail generator with progress reporting
2. Create a JSON config editor with file monitoring
3. Implement a thread-safe logging system for background tasks
❤4
By combining all the code from the steps above into
Results:
Data Persistence: Your inventory and invoice data is saved in
Integrated Workflow: Adding a purchase directly increases stock. A sale checks for and decreases stock. Production consumes raw materials and creates finished goods, all reflected in the central inventory table.
Separation of Concerns: The UI logic in
Reporting: You can easily export a snapshot of your current inventory to a CSV file for analysis in other programs like Excel or Google Sheets.
Discussion and Next Steps:
Scalability: While SQLite is excellent for small-to-medium applications, a large-scale, multi-user system would benefit from a client-server database like PostgreSQL or MySQL.
Invoice Complexity: The current invoice system is simplified. A real system would allow multiple items per invoice and store historical invoice data for viewing and printing.
User Interface (UI/UX): The UI is functional but could be greatly improved with better layouts, icons, search/filter functionality in tables, and more intuitive workflows.
Error Handling: The error handling is basic. A production-grade app would have more comprehensive checks for user input and database operations.
• Advanced Features: Future additions could include user authentication, supplier and customer management, barcode scanning, and more detailed financial reporting.
This project forms a powerful template for building custom internal business tools with Python.
#ProjectComplete #SoftwareEngineering #ERP #PythonGUI #BusinessApp
━━━━━━━━━━━━━━━
By: @DataScience4 ✨
database.py and main.py, you have a robust, database-driven desktop application.Results:
Data Persistence: Your inventory and invoice data is saved in
warehouse.db and will be there when you restart the application.Integrated Workflow: Adding a purchase directly increases stock. A sale checks for and decreases stock. Production consumes raw materials and creates finished goods, all reflected in the central inventory table.
Separation of Concerns: The UI logic in
main.py is cleanly separated from the data logic in database.py, making the code easier to maintain and extend.Reporting: You can easily export a snapshot of your current inventory to a CSV file for analysis in other programs like Excel or Google Sheets.
Discussion and Next Steps:
Scalability: While SQLite is excellent for small-to-medium applications, a large-scale, multi-user system would benefit from a client-server database like PostgreSQL or MySQL.
Invoice Complexity: The current invoice system is simplified. A real system would allow multiple items per invoice and store historical invoice data for viewing and printing.
User Interface (UI/UX): The UI is functional but could be greatly improved with better layouts, icons, search/filter functionality in tables, and more intuitive workflows.
Error Handling: The error handling is basic. A production-grade app would have more comprehensive checks for user input and database operations.
• Advanced Features: Future additions could include user authentication, supplier and customer management, barcode scanning, and more detailed financial reporting.
This project forms a powerful template for building custom internal business tools with Python.
#ProjectComplete #SoftwareEngineering #ERP #PythonGUI #BusinessApp
━━━━━━━━━━━━━━━
By: @DataScience4 ✨