Hệ thống quản lý thư viện với Python GUI - Module Quản lý Bạn đọc
- Giới thiệu
- Tính năng
- Công nghệ
- Yêu cầu hệ thống
- Cài đặt
- Cấu hình
- Sử dụng
- Cấu trúc dự án
- Database Schema
- Screenshots
- API Documentation
- Troubleshooting
- Roadmap
- Đóng góp
- License
- Tác giả
Library Management System là một ứng dụng desktop được phát triển bằng Python, sử dụng Tkinter cho giao diện đồ họa và MySQL làm cơ sở dữ liệu. Dự án hiện tại tập trung vào module Quản lý Bạn đọc (Reader Management) với đầy đủ các tính năng CRUD, tìm kiếm, lọc, thống kê và xuất dữ liệu.
- ✅ Kiến trúc MVC chuẩn, dễ mở rộng
- ✅ OOP đầy đủ, code sạch và có tổ chức
- ✅ MySQL Connection Pooling tối ưu hiệu suất
- ✅ Validation dữ liệu đầu vào chi tiết
- ✅ Exception Handling toàn diện
- ✅ Logging system hoàn chỉnh
- ✅ Export đa định dạng (JSON, CSV, Excel, PDF)
- ✅ UI/UX thân thiện, dễ sử dụng
- ➕ Thêm bạn đọc mới với validation đầy đủ
- ✏️ Sửa thông tin bạn đọc
- 🗑️ Xóa bạn đọc (kiểm tra ràng buộc)
- 👁️ Xem chi tiết thông tin
- 🔍 Tìm kiếm theo nhiều tiêu chí:
- Họ tên
- Số điện thoại
- Địa chỉ
- Tất cả (tìm kiếm toàn văn)
- 🔎 Lọc theo:
- Trạng thái (Active/Expired/Locked)
- Điểm uy tín (từ X đến Y)
- Thẻ sắp hết hạn (30 ngày)
- 🔒 Khóa/Mở khóa bạn đọc
- 📅 Gia hạn thẻ linh hoạt
- 🔄 Auto-update trạng thái hết hạn
⚠️ Cảnh báo thẻ sắp hết hạn
- 📊 Dashboard tổng quan
- 📈 Thống kê theo:
- Tổng số bạn đọc
- Phân loại trạng thái
- Điểm uy tín trung bình
- Thẻ sắp hết hạn
- 📉 Biểu đồ trực quan
- 📄 JSON - Dữ liệu có cấu trúc
- 📊 CSV - Import vào Excel
- 📗 Excel (. xlsx) - Báo cáo đẹp với định dạng
- 📕 PDF - In ấn và lưu trữ
- ⌨️ Keyboard shortcuts đầy đủ
- 🖱️ Context menu (chuột phải)
- 🔄 Auto-refresh thông minh
- 🎨 Color coding theo trạng thái
- 📱 Responsive layout
- ⚡ Fast search (debounced)
- 📚 Quản lý Sách (Books)
- 📋 Mượn/Trả sách (Borrow/Return)
- 💰 Quản lý Phạt (Penalties)
- 👥 Quản lý Nhân viên (Staff)
- 📊 Báo cáo tổng hợp
- Python 3.8+
- MySQL 8.0+ (với Connection Pooling)
- mysql-connector-python 8.2.0
- Tkinter (GUI Framework)
- ttk (Themed widgets)
- openpyxl - Excel export
- reportlab - PDF export
- python-dotenv - Environment variables
- MVC Pattern (Model-View-Controller)
- OOP (Object-Oriented Programming)
- Service Layer pattern
- MySQL 8.0+
- Connection Pooling
- Prepared Statements (SQL Injection prevention)
- Python 3.8 hoặc cao hơn
- MySQL 8.0 hoặc cao hơn
- pip (Python package manager)
- ✅ Windows 10/11
- ✅ macOS 10.14+
- ✅ Linux (Ubuntu 20.04+, Fedora 35+)
- CPU: 2 cores trở lên
- RAM: 4GB trở lên
- Disk: 100MB trống (cho ứng dụng + data)
git clone https://github.com/NvkhoaDev54/library-management.git
cd library-management# Tạo virtual environment
python -m venv venv
# Kích hoạt virtual environment
# Windows (Command Prompt)
venv\Scripts\activate
# Windows (PowerShell)
venv\Scripts\Activate. ps1
# macOS/Linux
source venv/bin/activatepip install -r requirements.txtrequirements.txt:
mysql-connector-python==8.2.0
openpyxl==3.1.2
reportlab==4.0.7
python-dotenv==1.0.0- Download MySQL Community Server
- Chạy installer và làm theo hướng dẫn
- Ghi nhớ root password
brew install mysql
brew services start mysql
mysql_secure_installationsudo apt update
sudo apt install mysql-server
sudo systemctl start mysql
sudo mysql_secure_installation# Đăng nhập MySQL
mysql -u root -p
# Hoặc chạy file SQL trực tiếp
mysql -u root -p < library_management.sqlHoặc trong MySQL shell:
source library_management.sql;Tạo file .env trong thư mục gốc:
# Copy từ template
cp .env.example .env
# Chỉnh sửa file . env
nano .env # hoặc notepad .env (Windows)Nội dung .env:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password_here
DB_NAME=library_management
APP_NAME=Library Management System
APP_VERSION=1.0.0
DEBUG=True
⚠️ Lưu ý: Thayyour_password_herebằng password MySQL của bạn
python main.pyclass DatabaseConfig:
HOST = '127.0.0.1'
PORT = 3306
USER = 'root'
PASSWORD = 'your_password'
DATABASE = 'library_management'
# Connection Pool
POOL_SIZE = 10
POOL_NAME = 'library_pool'class AppConfig:
APP_NAME = 'Library Management System'
VERSION = '1.0.0'
DEBUG = True
# Thư mục
EXPORT_DIR = 'data/export'
TEMP_DIR = 'data/temp'
# Settings
DEFAULT_CARD_VALIDITY_DAYS = 365
MAX_SEARCH_RESULTS = 1000python main.pySau khi khởi động, bạn sẽ thấy:
- Header: Logo và thông tin phiên bản
- Menu bar: File, Quản lý, Báo cáo, Công cụ, Trợ giúp
- Tabs: Quản lý Bạn đọc, Sách, Mượn/Trả...
- Status bar: Trạng thái kết nối, thời gian
- Click nút "➕ Thêm mới"
- Điền thông tin:
- Họ tên (bắt buộc)
- Số điện thoại
- Địa chỉ
- Ngày cấp thẻ / Ngày hết hạn
- Trạng thái
- Điểm uy tín
- Click "💾 Lưu"
- Double-click vào dòng cần sửa
- Hoặc chọn dòng → Click "✏️ Sửa"
- Chỉnh sửa thông tin
- Click "💾 Lưu"
- Chọn dòng cần xóa
- Nhấn Delete hoặc Click "🗑️ Xóa"
- Xác nhận xóa
- Nhập từ khóa vào ô tìm kiếm
- Chọn tiêu chí: Tất cả / Họ tên / SĐT / Email / Địa chỉ
- Nhấn Enter hoặc click "🔍 Tìm"
- Chọn trạng thái: Active / Expired / Locked
- Đặt khoảng điểm uy tín: Từ X đến Y
- Check "Sắp hết hạn (30 ngày)" nếu cần
- Click "🔎 Lọc"
- Click nút xuất: JSON / CSV / Excel / PDF
- File sẽ được lưu vào
data/export/ - Thông báo hiển thị đường dẫn file
| Phím | Chức năng |
|---|---|
F5 |
Làm mới dữ liệu |
Ctrl+Q |
Thoát ứng dụng |
Ctrl+1 |
Tab Bạn đọc |
Ctrl+2 |
Tab Sách |
Ctrl+3 |
Tab Mượn/Trả |
Delete |
Xóa dòng được chọn |
Enter |
Lưu (trong dialog) |
Esc |
Hủy (trong dialog) |
- ✏️ Sửa
- 🗑️ Xóa
- 🔒 Khóa
- 🔓 Mở khóa
- 📅 Gia hạn thẻ
- ℹ️ Chi tiết
library_management/
│
├── main.py # Entry point
├── requirements.txt # Dependencies
├── . env # Config (không commit)
├── .env.example # Config template
├── . gitignore # Git ignore rules
├── README.md # Documentation
├── library_management.sql # Database schema
│
├── config/ # Configuration
│ ├── __init__.py
│ ├── database.py # MySQL connection pool
│ └── settings.py # App settings
│
├── models/ # Data Models
│ ├── __init__.py
│ └── reader.py # Reader model
│
├── controllers/ # Controllers
│ ├── __init__.py
│ └── reader_controller.py # Reader controller
│
├── views/ # GUI Views
│ ├── __init__.py
│ ├── main_window.py # Main window
│ ├── reader_view.py # Reader management view
│ └── reader_dialog.py # Add/Edit dialog
│
├── services/ # Business Logic
│ ├── __init__.py
│ └── reader_service.py # Reader service
│
├── utils/ # Utilities
│ ├── __init__.py
│ ├── validators.py # Input validation
│ ├── messagebox_helper.py # Message boxes
│ └── export_helper.py # Export functions
│
├── data/ # Data directory
│ ├── . gitkeep
│ ├── library. db # SQLite (optional)
│ ├── export/ # Exported files
│ │ └── . gitkeep
│ └── temp/ # Temporary files
│ └── .gitkeep
│
└── logs/ # Log files
├── . gitkeep
└── app.log # Application log
CREATE TABLE readers (
reader_id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(150) NOT NULL,
address VARCHAR(255),
phone VARCHAR(20),
email VARCHAR(100),
card_start DATE,
card_end DATE,
status ENUM('ACTIVE','EXPIRED','LOCKED') DEFAULT 'ACTIVE',
reputation_score INT DEFAULT 100
);-- Index cho tìm kiếm nhanh
CREATE INDEX idx_full_name ON readers(full_name);
CREATE INDEX idx_phone ON readers(phone);
CREATE INDEX idx_email ON readers(email);
CREATE INDEX idx_status ON readers(status);
CREATE INDEX idx_card_end ON readers(card_end);
-- Index composite
CREATE INDEX idx_status_card ON readers(status, card_end);readers (1) ----< (N) borrow_slips
readers (1) ----< (N) penalties


💡 Lưu ý: Screenshots sẽ được thêm vào sau
Thêm bạn đọc mới vào database.
Parameters:
reader(Reader): Object chứa thông tin bạn đọc
Returns:
Tuple[bool, Optional[str], Optional[int]]: (success, error_message, reader_id)
Example:
reader = Reader(
full_name="Nguyễn Văn A",
phone="0901234567",
email="nguyenvana@example.com",
card_start="2025-01-01",
card_end="2026-01-01",
status="ACTIVE",
reputation_score=100
)
success, error, reader_id = service.create_reader(reader)
if success:
print(f"Created reader with ID: {reader_id}")Lấy danh sách tất cả bạn đọc.
Returns:
List[Reader]: Danh sách bạn đọc
Tìm kiếm bạn đọc theo từ khóa.
Parameters:
keyword(str): Từ khóa tìm kiếmsearch_by(str): Tiêu chí ('all', 'name', 'phone', 'email', 'address')
Returns:
List[Reader]: Danh sách kết quả
Error 1045 (28000): Access denied for user 'xxx'@'localhost'
Giải pháp:
- Kiểm tra username/password trong
.env - Tạo user và cấp quyền:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON library_management.* TO 'username'@'localhost';
FLUSH PRIVILEGES;ModuleNotFoundError: No module named 'mysql'
Giải pháp:
pip install -r requirements.txtError 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306'
Giải pháp:
- Kiểm tra MySQL đang chạy:
# Windows
sc query MySQL80
# macOS/Linux
sudo systemctl status mysql- Kiểm tra port trong
.env(3306 hoặc 3307)
Giải pháp: Đảm bảo tất cả file Python có encoding UTF-8:
# -*- coding: utf-8 -*-Giải pháp:
- Kiểm tra log:
logs/app.log - Chạy với debug mode:
python -u main.py- Module Quản lý Sách
- Module Mượn/Trả sách
- Tích hợp Barcode scanner
- Module Quản lý Nhân viên
- Hệ thống đăng nhập
- Phân quyền người dùng (Roles)
- Module Phạt và Thanh toán
- In thẻ bạn đọc
- Email notifications
- Web version (Flask/Django)
- Mobile app (React Native)
- REST API
Mọi đóng góp đều được hoan nghênh! Hãy làm theo các bước sau:
- Fork repository
- Clone về máy local:
git clone https://github.com/your-username/library-management.git- Tạo branch mới:
git checkout -b feature/amazing-feature- Commit changes:
git commit -m "Add some amazing feature"- Push lên branch:
git push origin feature/amazing-feature- Mở Pull Request
- Tuân thủ PEP 8 Python style guide
- Viết docstrings cho functions/classes
- Thêm type hints khi có thể
- Viết unit tests cho code mới
- Cập nhật README nếu cần
Nếu phát hiện bug, hãy tạo Issue với:
- Mô tả chi tiết bug
- Các bước tái hiện
- Expected vs Actual behavior
- Screenshots (nếu có)
- Environment (OS, Python version, MySQL version)
Dự án này được phân phối dưới giấy phép MIT License.
MIT License
Copyright (c) 2025 NvkhoaDev54
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Chi tiết: LICENSE
NvkhoaDev54
- GitHub: @NvkhoaDev54
- Email: nvkhoadev54@example.com
- LinkedIn: Your LinkedIn
- Python Software Foundation
- MySQL
- [Tkinter Documentation](https://docs.python.org/3/library/tkinter. html)
- ReportLab
- OpenPyXL
Nếu bạn cần hỗ trợ hoặc có câu hỏi:
- 📧 Email: nvkhoadev54@example.com
- 💬 Discord: Join our server
- 🐛 Issues: GitHub Issues
- 📖 Wiki: Project Wiki
**Nếu thấy project hữu ích, hãy cho một ⭐ nhé! **
Made with ❤️ by NvkhoaDev54


