Solution
import tkinter as tk
from tkinter import ttk
class MarksheetCalculator:
def __init__(self, root):
self.root = root
self.root.title("Marksheet Calculator")
self.subjects = []
self.marks = []
self.create_widgets()
def create_widgets(self):
# Labels and Entry for Subjects and Marks
subject_label = ttk.Label(self.root, text="Enter Subjects:")
subject_label.grid(row=0, column=0, padx=10, pady=10, sticky=tk.W)
self.subject_entry = ttk.Entry(self.root)
self.subject_entry.grid(row=0, column=1, padx=10, pady=10)
marks_label = ttk.Label(self.root, text="Enter Marks:")
marks_label.grid(row=1, column=0, padx=10, pady=10, sticky=tk.W)
self.marks_entry = ttk.Entry(self.root)
self.marks_entry.grid(row=1, column=1, padx=10, pady=10)
add_button = ttk.Button(self.root, text="Add Subject", command=self.add_subject)
add_button.grid(row=1, column=2, padx=10, pady=10)
# Display Subjects and Marks
self.tree = ttk.Treeview(self.root, columns=("Subject", "Marks"), show="headings")
self.tree.heading("Subject", text="Subject")
self.tree.heading("Marks", text="Marks")
self.tree.grid(row=2, column=0, columnspan=3, padx=10, pady=10)
# Calculate Button
calculate_button = ttk.Button(self.root, text="Calculate", command=self.calculate_result)
calculate_button.grid(row=3, column=0, columnspan=3, pady=10)
# Result Label
self.result_label = ttk.Label(self.root, text="")
self.result_label.grid(row=4, column=0, columnspan=3, pady=10)
def add_subject(self):
subject = self.subject_entry.get()
marks = self.marks_entry.get()
if subject and marks:
self.subjects.append(subject)
self.marks.append(int(marks))
self.tree.insert("", "end", values=(subject, marks))
# Clear entry fields
self.subject_entry.delete(0, tk.END)
self.marks_entry.delete(0, tk.END)
def calculate_result(self):
total_marks = sum(self.marks)
average_marks = total_marks / len(self.marks)
# Determine the grade based on the average
if average_marks >= 90:
grade = "A+"
elif 80 <= average_marks < 90:
grade = "A"
elif 70 <= average_marks < 80:
grade = "B"
elif 60 <= average_marks < 70:
grade = "C"
elif 50 <= average_marks < 60:
grade = "D"
else:
grade = "F"
result_text = f"Total Marks: {total_marks}\nAverage Marks: {average_marks:.2f}\nGrade: {grade}"
self.result_label.config(text=result_text)
# Create the main window
root = tk.Tk()
# Create the MarksheetCalculator instance
marksheet_calculator = MarksheetCalculator(root)
# Start the Tkinter event loop
root.mainloop()