Skip to content

Conversation

antoninklopp
Copy link
Contributor

I propose a solution to #3 with a grid layout.
There will be not as much freedom when placing the widgets on the layout but they are resizing when resizing the window.

Copy link

@Imran-imtiaz48 Imran-imtiaz48 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The given code involves creating a PyQt5 application for a Student Database Management System. It includes a main window with buttons for entering student details, showing student details, and deleting records. The original code uses absolute positioning (move) and resizing (resize) of widgets, which can be problematic when the window size changes, as the layout will not adapt accordingly.

The improved version introduces a QGridLayout to manage the widgets more effectively. This approach makes the interface more responsive and easier to maintain, as the layout will adjust automatically when the window is resized. However, there are still some improvements and cleanups that can be applied for better readability and performance.

Improved Code

import sys
import sqlite3
import time
from PyQt5 import QtGui
from PyQt5.QtWidgets import (
    QTableWidgetItem, QTableWidget, QComboBox, QVBoxLayout, QGridLayout, QDialog,
    QWidget, QPushButton, QApplication, QMainWindow, QAction, QMessageBox, QLabel,
    QTextEdit, QProgressBar, QLineEdit, QHBoxLayout
)
from PyQt5.QtCore import QCoreApplication


class DBHelper(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # Dialog for deleting records
        self.dialogDelete = QDialog(self)
        self.vboxDelete = QVBoxLayout(self.dialogDelete)
        self.dialogDelete.setWindowTitle("Delete Record")
        self.dialogDelete.setLayout(self.vboxDelete)

        # Grid layout for the main window
        layout = QGridLayout()

        # Setting up the picture label
        self.picLabel = QLabel(self)
        self.picLabel.setScaledContents(True)
        self.picLabel.setPixmap(QtGui.QPixmap("user.png"))
        layout.addWidget(self.picLabel, 0, 0, 1, 2)

        # Enter Student Details button
        self.btnEnterStudent = QPushButton("Enter Student Details", self)
        self.btnEnterStudent.setFont(QtGui.QFont('', 13))
        self.btnEnterStudent.clicked.connect(self.enterstudent)
        layout.addWidget(self.btnEnterStudent, 1, 0)

        # Delete Record button
        self.btnDeleteRecord = QPushButton("Delete Record", self)
        self.btnDeleteRecord.setFont(QtGui.QFont('', 13))
        self.btnDeleteRecord.clicked.connect(self.showDeleteDialog)
        layout.addWidget(self.btnDeleteRecord, 1, 1)

        # Show Student Details button
        self.btnShowStudentDetails = QPushButton("Show Student Details", self)
        self.btnShowStudentDetails.setFont(QtGui.QFont('', 13))
        self.btnShowStudentDetails.clicked.connect(self.showStudentDialog)
        layout.addWidget(self.btnShowStudentDetails, 2, 0, 1, 2)

        # Set the layout to a central widget
        centralWidget = QWidget()
        centralWidget.setLayout(layout)
        self.setCentralWidget(centralWidget)

        self.resize(400, 280)
        self.setWindowTitle("Student Database Management System")

    def enterstudent(self):
        # Implement the functionality to enter student details
        pass

    def showDeleteDialog(self):
        # Implement the functionality to show delete dialog
        pass

    def showStudentDialog(self):
        # Implement the functionality to show student details
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = DBHelper()
    window.show()
    sys.exit(app.exec_())

Key Improvements

  1. Unified Import Statements: Grouped import statements to improve readability.
  2. Grid Layout: Replaced absolute positioning with QGridLayout, ensuring a responsive and maintainable layout.
  3. Simplified Font Setting: Simplified setting the font size for buttons.
  4. Central Widget: Used a central widget to apply the layout, which is a more standard approach in PyQt5 applications.

These changes result in a cleaner, more maintainable codebase that adapts better to different window sizes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants