Python | Algorithms | Data Structures | Cyber Security | Networks
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:
### 2. Custom Dialog Classes
Create reusable dialog windows:
---
## 🔹 File System Operations
### 1. File and Directory Handling
### 2. Monitoring File Changes
---
## 🔹 Drag and Drop
### 1. Enabling Drag-and-Drop
#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)