Code With Python
38.7K subscribers
792 photos
23 videos
21 files
718 links
This channel provides clear, practical content for developers focusing on Python, Django, data structures, algorithms, and DSA.

Admin: @Hussein_Sheikho

Ad & Earn money form your channel:
https://telega.io/?r=nikapsOH
Download Telegram
Code With Python
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)
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