Learn Python Coding
38.8K subscribers
608 photos
27 videos
22 files
366 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
Learn Python Coding
Photo
## 🔹 Multi-Window Applications
### 1. Creating Secondary Windows
class SettingsWindow(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Settings")
layout = QVBoxLayout()
layout.addWidget(QLabel("Application Settings"))
self.setLayout(layout)

# In main window:
def show_settings(self):
settings = SettingsWindow()
settings.exec_() # Modal dialog
# OR settings.show() for non-modal


### 2. Window Communication
# Main window with signal
class MainWindow(QMainWindow):
settings_changed = pyqtSignal(dict)

def open_settings(self):
dialog = SettingsDialog(self) # Pass parent
if dialog.exec_():
self.settings_changed.emit(dialog.get_settings())

# Settings dialog
class SettingsDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
# ... setup UI ...

def get_settings(self):
return {"theme": self.theme_combo.currentText()}


---

## 🔹 Model-View Architecture
### 1. QListView with StringListModel
model = QStringListModel()
model.setStringList(["Item 1", "Item 2", "Item 3"])

list_view = QListView()
list_view.setModel(model)

# Add items
model.insertRow(model.rowCount())
model.setData(model.index(model.rowCount()-1), "New Item")


### 2. Custom Table Model
class CustomTableModel(QAbstractTableModel):
def __init__(self, data):
super().__init__()
self._data = data

def rowCount(self, parent=None):
return len(self._data)

def columnCount(self, parent=None):
return len(self._data[0]) if self._data else 0

def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return str(self._data[index.row()][index.column()])
return None

# Usage
data = [[1, "Alice"], [2, "Bob"], [3, "Charlie"]]
model = CustomTableModel(data)
table = QTableView()
table.setModel(model)


---

## 🔹 Practical Example: Text Editor
class TextEditor(QMainWindow):
def __init__(self):
super().__init__()
self.setup_ui()
self.setup_menu()

def setup_ui(self):
self.text_edit = QTextEdit()
self.setCentralWidget(self.text_edit)

# Status bar
self.statusBar().showMessage("Ready")

# Toolbar
toolbar = self.addToolBar("Tools")
save_act = QAction(QIcon("save.png"), "Save", self)
save_act.triggered.connect(self.save_file)
toolbar.addAction(save_act)

def setup_menu(self):
menubar = self.menuBar()

# File menu
file_menu = menubar.addMenu("File")

open_act = QAction("Open", self)
open_act.triggered.connect(self.open_file)
file_menu.addAction(open_act)

# Edit menu
edit_menu = menubar.addMenu("Edit")
edit_menu.addAction("Copy", self.text_edit.copy)
edit_menu.addAction("Paste", self.text_edit.paste)

def open_file(self):
path, _ = QFileDialog.getOpenFileName()
if path:
with open(path, 'r') as f:
self.text_edit.setText(f.read())

def save_file(self):
path, _ = QFileDialog.getSaveFileName()
if path:
with open(path, 'w') as f:
f.write(self.text_edit.toPlainText())


---

## 🔹 Best Practices
1. Separate UI code from business logic
2. Use models for complex data views
3. Optimize performance for large datasets
4. Localize strings for internationalization
5. Document signals and public methods

---

### 📌 What's Next?
In Part 3, we'll cover:
➡️ Dialogs & Message Boxes
➡️ File System Operations
➡️ Drag & Drop
➡️ Threading with QThread

#PyQt5 #GUIPython #ProfessionalDevelopment 🚀

Practice Exercise:
1. Create a contacts app with tree view and detail form
2. Build a styled calculator with custom buttons
3. Implement a multi-window image viewer with thumbnails
Learn Python Coding
Photo
# 📚 PyQt5 Tutorial - Part 3/6: Dialogs, Files & Threading
#PyQt5 #Python #Threading #FileDialogs #DragAndDrop

Welcome to Part 3 of our PyQt5 series! This comprehensive lesson dives into professional dialog handling, file operations, drag-and-drop functionality, and threading - essential for building production-grade applications.

---

## 🔹 Professional Dialog Handling
### 1. Standard Dialogs
PyQt5 provides built-in dialogs for common tasks:

from PyQt5.QtWidgets import (QFileDialog, QColorDialog, 
QFontDialog, QInputDialog, QMessageBox)

# File Dialog
file_path, _ = QFileDialog.getOpenFileName(
self, "Open File", "", "Text Files (*.txt);;All Files (*)")

# Color Dialog
color = QColorDialog.getColor()

# Font Dialog
font, ok = QFontDialog.getFont()

# Input Dialog
text, ok = QInputDialog.getText(self, "Input", "Enter your name:")

# Message Box
reply = QMessageBox.question(
self, "Message", "Are you sure?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)


### 2. Custom Dialog Classes
Create reusable dialog windows:

class LoginDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Login")

self.username = QLineEdit()
self.password = QLineEdit()
self.password.setEchoMode(QLineEdit.Password)

buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)

layout = QFormLayout()
layout.addRow("Username:", self.username)
layout.addRow("Password:", self.password)
layout.addRow(buttons)

self.setLayout(layout)

def get_credentials(self):
return (self.username.text(), self.password.text())

# Usage
dialog = LoginDialog()
if dialog.exec_():
username, password = dialog.get_credentials()


---

## 🔹 File System Operations
### 1. File and Directory Handling
from PyQt5.QtCore import QDir, QFile, QFileInfo

# Check file existence
file_info = QFileInfo("path/to/file")
if file_info.exists():
print("File size:", file_info.size())

# Directory operations
directory = QDir()
directory.mkdir("new_folder")
print("Current path:", directory.currentPath())

# File reading/writing
file = QFile("data.txt")
if file.open(QIODevice.ReadOnly | QIODevice.Text):
stream = QTextStream(file)
content = stream.readAll()
file.close()


### 2. Monitoring File Changes
from PyQt5.QtCore import QFileSystemWatcher

class FileMonitor(QObject):
def __init__(self):
super().__init__()
self.watcher = QFileSystemWatcher()
self.watcher.fileChanged.connect(self.on_file_changed)

def add_file(self, path):
self.watcher.addPath(path)

def on_file_changed(self, path):
print(f"File changed: {path}")

monitor = FileMonitor()
monitor.add_file("important_file.txt")


---

## 🔹 Drag and Drop
### 1. Enabling Drag-and-Drop
class DropArea(QLabel):
def __init__(self):
super().__init__("Drop files here")
self.setAcceptDrops(True)
self.setAlignment(Qt.AlignCenter)
self.setStyleSheet("border: 2px dashed #aaa;")

def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()

def dropEvent(self, event):
for url in event.mimeData().urls():
file_path = url.toLocalFile()
print("Dropped file:", file_path)
1
Learn Python Coding
Photo

def show_error(self, message):
self.thread.quit()
QMessageBox.critical(self, "Error", message)

class FileWorker(QObject):
progress = pyqtSignal(int)
finished = pyqtSignal()
er