-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPA_CAL.py
211 lines (162 loc) · 8.54 KB
/
GPA_CAL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import tkinter as tk
from tkinter import messagebox, filedialog
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from theme import THEMES, apply_theme
class GPA_Calculator:
def __init__(self, root):
self.root = root
self.root.title("GPA Calculator")
self.theme_var = tk.StringVar(root)
self.theme_var.set("light") # Default theme
self.theme_menu = tk.OptionMenu(root, self.theme_var, *THEMES.keys(), command=self.change_theme)
self.theme_menu.grid(row=11, column=0, columnspan=2, pady=10)
self.student_name_label = tk.Label(root, text="Student Name:")
self.student_name_label.grid(row=0, column=0, padx=10, pady=10)
self.student_name_entry = tk.Entry(root)
self.student_name_entry.grid(row=0, column=1, padx=10, pady=10)
self.university_name_label = tk.Label(root, text="University Name:")
self.university_name_label.grid(row=1, column=0, padx=10, pady=10)
self.university_name_entry = tk.Entry(root)
self.university_name_entry.grid(row=1, column=1, padx=10, pady=10)
self.course_name_label = tk.Label(root, text="Course Name:")
self.course_name_label.grid(row=2, column=0, padx=10, pady=10)
self.course_name_entry = tk.Entry(root)
self.course_name_entry.grid(row=2, column=1, padx=10, pady=10)
self.credit_label = tk.Label(root, text="Credit Hours:")
self.credit_label.grid(row=3, column=0, padx=10, pady=10)
self.credit_entry = tk.Entry(root)
self.credit_entry.grid(row=3, column=1, padx=10, pady=10)
self.grade_label = tk.Label(root, text="Grade:")
self.grade_label.grid(row=4, column=0, padx=10, pady=10)
grades = ["A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "F"]
self.grade_var = tk.StringVar(root)
self.grade_var.set(grades[0])
self.grade_menu = tk.OptionMenu(root, self.grade_var, *grades)
self.grade_menu.grid(row=4, column=1, padx=10, pady=10)
self.add_button = tk.Button(root, text="Add Course", command=self.add_course)
self.add_button.grid(row=5, column=0, columnspan=1, pady=10)
self.delete_button = tk.Button(root, text="Delete Course", command=self.delete_course)
self.delete_button.grid(row=5, column=1, columnspan=1, pady=10)
self.clear_button = tk.Button(root, text="Clear All", command=self.clear_all)
self.clear_button.grid(row=10, column=0, columnspan=2, pady=10)
self.course_listbox = tk.Listbox(root, width=50, height=10)
self.course_listbox.grid(row=6, column=0, columnspan=2, pady=10)
self.total_credit_label = tk.Label(root, text="Total Credit Hours:")
self.total_credit_label.grid(row=7, column=0, padx=10, pady=10)
self.total_credit_value = tk.Label(root, text="0")
self.total_credit_value.grid(row=7, column=1, padx=10, pady=10)
self.overall_gpa_label = tk.Label(root, text="Overall GPA:")
self.overall_gpa_label.grid(row=8, column=0, padx=10, pady=10)
self.overall_gpa_value = tk.Label(root, text="0.0")
self.overall_gpa_value.grid(row=8, column=1, padx=10, pady=10)
self.export_button = tk.Button(root, text="Export to PDF", command=self.export_to_pdf)
self.export_button.grid(row=9, column=0, columnspan=2, pady=10)
self.total_credit_hours = 0
self.total_grade_points = 0
def add_course(self):
try:
course_name = self.course_name_entry.get()
credit = float(self.credit_entry.get())
grade = self.grade_var.get()
grade_points = self.convert_grade_to_points(grade) * credit
self.total_credit_hours += credit
self.total_grade_points += grade_points
course_info = f"Course: {course_name}, Credit: {credit}, Grade: {grade}"
self.course_listbox.insert(tk.END, course_info)
self.total_credit_value.config(text=str(self.total_credit_hours))
self.calculate_gpa()
# Clear entry fields
self.course_name_entry.delete(0, tk.END)
self.credit_entry.delete(0, tk.END)
self.grade_var.set("A+") # Set default grade to 'A+'
except ValueError:
messagebox.showerror("Error", "Invalid input. Please enter valid values for credit hours.")
def delete_course(self):
selected_index = self.course_listbox.curselection()
if selected_index:
# Extract credit from the selected course info
selected_info = self.course_listbox.get(selected_index)
_, credit, _ = [info.strip().split(":")[1] for info in selected_info.split(",")]
# Update total credit hours and total grade points
self.total_credit_hours -= float(credit)
self.total_grade_points -= self.convert_grade_to_points(self.grade_var.get()) * float(credit)
# Delete the selected course from the listbox
self.course_listbox.delete(selected_index)
# Update total credit hours label and recalculate overall GPA
self.total_credit_value.config(text=str(self.total_credit_hours))
self.calculate_gpa()
def calculate_gpa(self):
# Calculate overall GPA
if self.total_credit_hours > 0:
overall_gpa = self.total_grade_points / self.total_credit_hours
self.overall_gpa_value.config(text="{:.2f}".format(overall_gpa))
def clear_all(self):
# Clear all entry fields, labels, and course list
self.student_name_entry.delete(0, tk.END)
self.university_name_entry.delete(0, tk.END)
self.course_name_entry.delete(0, tk.END)
self.credit_entry.delete(0, tk.END)
self.grade_var.set("A+")
self.course_listbox.delete(0, tk.END)
self.total_credit_hours = 0
self.total_grade_points = 0
self.total_credit_value.config(text="0")
self.overall_gpa_value.config(text="0.0")
def change_theme(self, theme):
apply_theme(self.root, theme)
def convert_grade_to_points(self, grade):
grade_mapping = {
"A+": 4.0,
"A": 4.0,
"A-": 3.7,
"B+": 3.3,
"B": 3.0,
"B-": 2.7,
"C+": 2.3,
"C": 2.0,
"C-": 1.7,
"D+": 1.3,
"D": 1.0,
"F": 0.0
}
return grade_mapping.get(grade, 0.0)
def export_to_pdf(self):
try:
# Ask user for file name and location
file_path = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("PDF files", "*.pdf")])
if not file_path:
return # User canceled the file dialog
student_name = self.student_name_entry.get()
university_name = self.university_name_entry.get()
c = canvas.Canvas(file_path, pagesize=letter)
c.setFont("Helvetica-Bold", 16)
c.drawCentredString(300, 750, "GPA Report")
c.setFont("Helvetica-Bold", 12)
c.drawString(100, 720, "Student Name: {}".format(student_name))
c.drawString(100, 700, "University Name: {}".format(university_name))
c.drawString(100, 680, "Total Credit Hours: {}".format(self.total_credit_hours))
c.drawString(100, 660, "Overall GPA: {:.2f}".format(self.total_grade_points / self.total_credit_hours))
c.setFont("Helvetica-Bold", 12)
c.drawString(100, 620, "Course")
c.drawString(250, 620, "Credit")
c.drawString(400, 620, "Grade")
c.setFont("Helvetica", 12)
y_position = 600
for course_info in self.course_listbox.get(0, tk.END):
# Split the course info string to extract course name, credit, and grade
course_name, credit, grade = [info.strip().split(":")[1] for info in course_info.split(",")]
# Write course details to the PDF
c.drawString(100, y_position, course_name)
c.drawString(250, y_position, credit)
c.drawString(400, y_position, grade)
y_position -= 20
c.save()
messagebox.showinfo("Export Successful", "GPA report exported to {}".format(file_path))
except Exception as e:
messagebox.showerror("Error", "An error occurred while exporting to PDF: {}".format(str(e)))
if __name__ == "__main__":
root = tk.Tk()
app = GPA_Calculator(root)
apply_theme(root, "light") # Apply the default theme
root.mainloop()