Skip to content

Commit

Permalink
Merge pull request #20 from Kr-Adarsh/main
Browse files Browse the repository at this point in the history
Converted CLI to GUI
  • Loading branch information
qaidjoharj53 authored Oct 7, 2024
2 parents 9eb724f + ed365bf commit c2805ad
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 7 deletions.
77 changes: 77 additions & 0 deletions GUI/GUI_student.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from PyQt5.QtWidgets import *
from PyQt5 import uic
import student
import file_operations
import os # Import os for file operations


class Mygui(QMainWindow):

def __init__(self):
super(Mygui, self).__init__()
uic.loadUi("first_gui.ui", self)

self.show()

# Connect buttons to their respective methods
self.pushButton.clicked.connect(self.add_record) # Record Button
self.pushButton_2.clicked.connect(self.display_records) # Display Records Button
self.pushButton_3.clicked.connect(self.delete_file) # Delete All Records Button

def add_record(self):
# Retrieve input values from the GUI
roll = self.lineEdit.text()
name = self.lineEdit_2.text()
percentage = self.lineEdit_3.text()

# Ensure the inputs are valid (check if fields are not empty and percentage is numeric)
if roll.isdigit() and percentage.replace('.', '', 1).isdigit():
# Create a Student object
student_obj = student.Student()
student_obj.add_record(int(roll), name, float(percentage))

# Write the student record to the file using file_operations
result = file_operations.write_record(student_obj)
self.statusBar().showMessage(result) # Display result message in the status bar

# Clear the input fields after adding the record
self.lineEdit.clear()
self.lineEdit_2.clear()
self.lineEdit_3.clear()
else:
self.statusBar().showMessage("Invalid input. Please check the fields.")

def display_records(self):
# Fetch all records from the file using file_operations
records = file_operations.display_all_records()

# Clear the textEdit area before displaying new records
self.textEdit.clear()

# Display all records in the textEdit widget
if records:
for record in records:
self.textEdit.append(record + "\n") # Display each record and add a new line
else:
self.textEdit.append("No records found.")

def delete_file(self):
try:
if os.path.exists("stud.dat"):
os.remove("stud.dat") # Delete the file
self.statusBar().showMessage("All records deleted successfully!")
self.textEdit.clear() # Clear the text area if needed
else:
self.statusBar().showMessage("File not found!")
except Exception as e:
self.statusBar().showMessage(f"Error: {str(e)}")


def main():
app = QApplication([])
window = Mygui()
app.exec_()


if __name__ == '__main__':
main()
31 changes: 24 additions & 7 deletions GUI/file_operations.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# file_operations.py
import pickle

import os
import student

def write_record(student):
try:
with open("stud.dat", "ab") as file:
pickle.dump(student, file)
pickle.dump(student, file) # Ensure student is a Student instance
return "Record added in file!"
except Exception as e:
return f"Error: {str(e)}"
Expand All @@ -16,10 +17,26 @@ def display_all_records():
try:
with open("stud.dat", "rb") as file:
while True:
student = pickle.load(file)
records.append(student.display_record())
return records
except EOFError:
return records
try:
student = pickle.load(file) # Load the Student object
records.append(student.display_record()) # Append the student's record
except EOFError:
break
return records if records else ["No records found."]
except FileNotFoundError:
return ["File not found! Please add a record first."]
except pickle.UnpicklingError:
return ["Error in unpickling data! The file might be corrupted."]
except IOError:
return ["File could not be opened!"]

def delete_file(self):
try:
if os.path.exists("stud.dat"):
os.remove("stud.dat") # Delete the file
self.statusBar().showMessage("All records deleted successfully!")
self.textEdit.clear() # Clear the text area if needed
else:
self.statusBar().showMessage("File not found!")
except Exception as e:
self.statusBar().showMessage(f"Error: {str(e)}")
163 changes: 163 additions & 0 deletions GUI/first_gui.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>462</width>
<height>593</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>121</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Roll Number: </string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>121</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Name</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>10</x>
<y>80</y>
<width>121</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Percentage: </string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>210</x>
<y>20</y>
<width>201</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>210</x>
<y>50</y>
<width>201</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_3">
<property name="geometry">
<rect>
<x>210</x>
<y>80</y>
<width>201</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>70</x>
<y>120</y>
<width>281</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Add Record</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>180</y>
<width>421</width>
<height>261</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>20</x>
<y>450</y>
<width>421</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>Display all Records</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>10</x>
<y>150</y>
<width>55</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Records:</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>20</x>
<y>500</y>
<width>421</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Delete the Data</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>462</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
19 changes: 19 additions & 0 deletions GUI/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Student Management System GUI

A simple Student Management System built using PyQt5 and Python for managing student records. The application allows users to add, display, and delete student records.

## Features

- **Add Student Record**: Enter roll number, name, and percentage to add a new student record.
- **Display Records**: View all student records stored in a file.
- **Delete Records**: Delete all records and reset the database by removing the `stud.dat` file.

## Technologies Used

- **Python**: Programming language used for development.
- **PyQt5**: GUI toolkit for creating the application interface.
- **Pickle**: Used for serializing and deserializing Python objects.

## To run the GUI:

> python GUI_student.py
Binary file removed GUI/stud.dat
Binary file not shown.

0 comments on commit c2805ad

Please sign in to comment.