Learn Python Coding
39.3K subscribers
649 photos
34 videos
24 files
418 links
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Learn Python Coding
Photo
# 📚 PyQt5 Tutorial - Part 1/6: Introduction to GUI Programming
#PyQt5 #Python #GUI #BeginnerFriendly #Qt

Welcome to Part 1 of our comprehensive PyQt5 series! This lesson will introduce you to GUI development with Python and PyQt5, perfect for beginners.

---

## 🔹 What is PyQt5?
PyQt5 is a set of Python bindings for Qt (a powerful C++ GUI framework). It lets you create:
- Desktop applications
- Cross-platform GUIs
- Professional-looking interfaces
- Apps with databases, networking, and multimedia

Key Features:
✔️ 620+ classes
✔️ 6,000+ functions
✔️ Windows, Mac, Linux support
✔️ Open-source (GPL/commercial licenses)

---

## 🔹 Installation
Install PyQt5 and tools:

pip install PyQt5 PyQt5-tools


Verify Installation:
import PyQt5
print(PyQt5.__version__) # Should show version like 5.15.4


---

## 🔹 Your First PyQt5 App
Let's create a simple window:

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget

# 1. Create the application object
app = QApplication(sys.argv)

# 2. Create main window
window = QWidget()
window.setWindowTitle("My First App")
window.setGeometry(100, 100, 400, 200) # x, y, width, height

# 3. Add a label
label = QLabel("Hello PyQt5!", parent=window)
label.move(150, 80) # x, y position

# 4. Show the window
window.show()

# 5. Run the application
sys.exit(app.exec_())


Code Breakdown:
1. QApplication: Manages app control flow
2. QWidget: Base class for all UI objects
3. QLabel: Displays text/images
4. exec_(): Starts the event loop

---

## 🔹 Core PyQt5 Components
### 1. Main Window Types
| Class | Purpose |
|-------|---------|
| QWidget | Basic empty window |
| QMainWindow | With menu bar, status bar, toolbars |
| QDialog | Popup dialog windows |

### 2. Common Widgets
from PyQt5.QtWidgets import (
QPushButton, # Clickable button
QLineEdit, # Single-line text input
QTextEdit, # Multi-line text area
QCheckBox, # Toggle option
QRadioButton, # Exclusive choice
QComboBox, # Dropdown menu
QSlider # Value selector
)


### 3. Layout Managers
from PyQt5.QtWidgets import (
QVBoxLayout, # Vertical arrangement
QHBoxLayout, # Horizontal arrangement
QGridLayout # Grid arrangement
)


---

## 🔹 Creating a Functional App
Let's build a temperature converter:
1
Learn Python Coding
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