Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing a Registration Page with PyQt5 for a Library Management System

Tech 3

The registration interface includes fields for usernmae, password, and a registration button. The logic layer validates input upon clicking the register button, checking for existing accounts and ensuring username and password meet requirements.

A clean UI design can be implemented using QTCreator's drag-and-drop features and converted to Python code, or directly coded. The interface consists of a central label, four labels with input boxes for registration details, and a register button.

def setup_interface(self):
    header_font = QFont()
    header_font.setPixelSize(18)
    input_font = QFont()
    input_font.setPixelSize(16)
    
    main_layout = QVBoxLayout()
    self.setLayout(main_layout)
    
    registration_label = QLabel("Register")
    registration_label.setFixedHeight(100)
    registration_label.setAlignment(Qt.AlignCenter)
    registration_label.setFont(header_font)
    main_layout.addWidget(registration_label, Qt.AlignHCenter)
    
    form_layout = QFormLayout()
    
    account_label = QLabel("Account:")
    account_label.setFont(header_font)
    self.account_input = QLineEdit()
    self.account_input.setFixedWidth(180)
    self.account_input.setFixedHeight(32)
    self.account_input.setFont(input_font)
    self.account_input.setMaxLength(10)
    form_layout.addRow(account_label, self.account_input)
    
    name_label = QLabel("Name:")
    name_label.setFont(header_font)
    self.name_input = QLineEdit()
    self.name_input.setFixedHeight(32)
    self.name_input.setFixedWidth(180)
    self.name_input.setFont(input_font)
    self.name_input.setMaxLength(10)
    form_layout.addRow(name_label, self.name_input)
    
    password_label = QLabel("Password:")
    password_label.setFont(header_font)
    self.password_input = QLineEdit()
    self.password_input.setFixedWidth(180)
    self.password_input.setFixedHeight(32)
    self.password_input.setFont(input_font)
    self.password_input.setEchoMode(QLineEdit.PasswordEchoOnEdit)
    self.password_input.setMaxLength(16)
    form_layout.addRow(password_label, self.password_input)
    
    confirm_label = QLabel("Confirm Password:")
    confirm_label.setFont(header_font)
    self.confirm_input = QLineEdit()
    self.confirm_input.setFixedWidth(180)
    self.confirm_input.setFixedHeight(32)
    self.confirm_input.setFont(input_font)
    self.confirm_input.setEchoMode(QLineEdit.PasswordEchoOnEdit)
    self.confirm_input.setMaxLength(16)
    form_layout.addRow(confirm_label, self.confirm_input)
    
    self.register_btn = QPushButton("Register")
    self.register_btn.setFixedWidth(120)
    self.register_btn.setFixedHeight(30)
    self.register_btn.setFont(header_font)
    form_layout.addRow("", self.register_btn)
    
    form_widget = QWidget()
    form_widget.setLayout(form_layout)
    form_widget.setFixedHeight(250)
    form_widget.setFixedWidth(300)
    
    horizontal_layout = QHBoxLayout()
    horizontal_layout.addWidget(form_widget, Qt.AlignCenter)
    
    container_widget = QWidget()
    container_widget.setLayout(horizontal_layout)
    main_layout.addWidget(container_widget, Qt.AlignHCenter)
    
    account_validator = QRegExpValidator(self)
    account_validator.setRegExp(QRegExp("user[0-9]{8}"))
    self.account_input.setValidator(account_validator)
    
    password_validator = QRegExpValidator(self)
    password_validator.setRegExp(QRegExp("[a-zA-Z0-9]+$"))
    self.password_input.setValidator(password_validator)
    self.confirm_input.setValidator(password_validator)

During program initialization, the database is loaded and the UI interface is set up.

def __init__(self):
    super().__init__()
    self.resize(900, 600)
    self.setWindowTitle("Library Management System Registration")
    self.setup_interface()
    self.user_manager = UserDbManager()

When the register button is clicked, a signal triggers the registration function to handle the event.

def process_registration(self):
    account = self.account_input.text()
    name = self.name_input.text()
    password = self.password_input.text()
    confirm = self.confirm_input.text()
    
    if not account or not name or not password or not confirm:
        QMessageBox.warning(self, "Warning", "All fields must be completed.", QMessageBox.Yes)
        return
    
    if password != confirm:
        QMessageBox.warning(self, "Warning", "Passwords do not match.", QMessageBox.Yes)
        return
    
    hasher = hashlib.md5()
    hasher.update(password.encode('utf-8'))
    encrypted_password = hasher.hexdigest()
    
    existing_user = self.user_manager.query_by_account(account)
    
    if existing_user:
        QMessageBox.warning(self, "Warning", "Account already exists.", QMessageBox.Yes)
        return
    
    self.user_manager.add_user(account, name, encrypted_password)
    
    if self.user_manager.query_by_account(account):
        QMessageBox.information(self, "Success", "Registration completed successfully.", QMessageBox.Yes)
        self.registration_signal.emit(account)
    else:
        QMessageBox.warning(self, "Error", "Registration failed. Please try again.", QMessageBox.Yes)

This code can run independently. To integrate with other applications, modify the database operations in the registration function accordingly.

Complete program:

SignUp.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import hashlib
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
from db.userInfoManager import UserDbManager

class RegistrationWidget(QWidget):
    registration_signal = pyqtSignal(str)
    
    def __init__(self):
        super().__init__()
        self.resize(900, 600)
        self.setWindowTitle("Library Management System Registration")
        self.setup_interface()
        self.user_manager = UserDbManager()
    
    def setup_interface(self):
        header_font = QFont()
        header_font.setPixelSize(18)
        input_font = QFont()
        input_font.setPixelSize(16)
        
        main_layout = QVBoxLayout()
        self.setLayout(main_layout)
        
        registration_label = QLabel("Register")
        registration_label.setFixedHeight(100)
        registration_label.setAlignment(Qt.AlignCenter)
        registration_label.setFont(header_font)
        main_layout.addWidget(registration_label, Qt.AlignHCenter)
        
        form_layout = QFormLayout()
        
        account_label = QLabel("Account:")
        account_label.setFont(header_font)
        self.account_input = QLineEdit()
        self.account_input.setFixedWidth(180)
        self.account_input.setFixedHeight(32)
        self.account_input.setFont(input_font)
        self.account_input.setMaxLength(10)
        form_layout.addRow(account_label, self.account_input)
        
        name_label = QLabel("Name:")
        name_label.setFont(header_font)
        self.name_input = QLineEdit()
        self.name_input.setFixedHeight(32)
        self.name_input.setFixedWidth(180)
        self.name_input.setFont(input_font)
        self.name_input.setMaxLength(10)
        form_layout.addRow(name_label, self.name_input)
        
        password_label = QLabel("Password:")
        password_label.setFont(header_font)
        self.password_input = QLineEdit()
        self.password_input.setFixedWidth(180)
        self.password_input.setFixedHeight(32)
        self.password_input.setFont(input_font)
        self.password_input.setEchoMode(QLineEdit.PasswordEchoOnEdit)
        self.password_input.setMaxLength(16)
        form_layout.addRow(password_label, self.password_input)
        
        confirm_label = QLabel("Confirm Password:")
        confirm_label.setFont(header_font)
        self.confirm_input = QLineEdit()
        self.confirm_input.setFixedWidth(180)
        self.confirm_input.setFixedHeight(32)
        self.confirm_input.setFont(input_font)
        self.confirm_input.setEchoMode(QLineEdit.PasswordEchoOnEdit)
        self.confirm_input.setMaxLength(16)
        form_layout.addRow(confirm_label, self.confirm_input)
        
        self.register_btn = QPushButton("Register")
        self.register_btn.setFixedWidth(120)
        self.register_btn.setFixedHeight(30)
        self.register_btn.setFont(header_font)
        form_layout.addRow("", self.register_btn)
        
        form_widget = QWidget()
        form_widget.setLayout(form_layout)
        form_widget.setFixedHeight(250)
        form_widget.setFixedWidth(300)
        
        horizontal_layout = QHBoxLayout()
        horizontal_layout.addWidget(form_widget, Qt.AlignCenter)
        
        container_widget = QWidget()
        container_widget.setLayout(horizontal_layout)
        main_layout.addWidget(container_widget, Qt.AlignHCenter)
        
        account_validator = QRegExpValidator(self)
        account_validator.setRegExp(QRegExp("user[0-9]{8}"))
        self.account_input.setValidator(account_validator)
        
        password_validator = QRegExpValidator(self)
        password_validator.setRegExp(QRegExp("[a-zA-Z0-9]+$"))
        self.password_input.setValidator(password_validator)
        self.confirm_input.setValidator(password_validator)
        
        self.register_btn.clicked.connect(self.process_registration)
        self.account_input.returnPressed.connect(self.process_registration)
        self.name_input.returnPressed.connect(self.process_registration)
        self.password_input.returnPressed.connect(self.process_registration)
        self.confirm_input.returnPressed.connect(self.process_registration)
    
    def process_registration(self):
        account = self.account_input.text()
        name = self.name_input.text()
        password = self.password_input.text()
        confirm = self.confirm_input.text()
        
        if not account or not name or not password or not confirm:
            QMessageBox.warning(self, "Warning", "All fields must be completed.", QMessageBox.Yes)
            return
        
        if password != confirm:
            QMessageBox.warning(self, "Warning", "Passwords do not match.", QMessageBox.Yes)
            return
        
        hasher = hashlib.md5()
        hasher.update(password.encode('utf-8'))
        encrypted_password = hasher.hexdigest()
        
        existing_user = self.user_manager.query_by_account(account)
        
        if existing_user:
            QMessageBox.warning(self, "Warning", "Account already exists.", QMessageBox.Yes)
            return
        
        self.user_manager.add_user(account, name, encrypted_password)
        
        if self.user_manager.query_by_account(account):
            QMessageBox.information(self, "Success", "Registration completed successfully.", QMessageBox.Yes)
            self.registration_signal.emit(account)
        else:
            QMessageBox.warning(self, "Error", "Registration failed. Please try again.", QMessageBox.Yes)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    main_window = RegistrationWidget()
    main_window.show()
    sys.exit(app.exec_())
Tags: pyqt5Python

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.