Introduction
Grading systems help evaluate student performance based on marks. In this post, we'll build a simple grade calculator using Tkinter in Python. The user enters marks, clicks a button, and the program displays the grade.
Code: Grade Calculator using Tkinter
from tkinter import *
def calculate_grade():
marks = float(e1.get())
if marks >= 90:
grade = "A"
elif marks >= 80:
grade = "B"
elif marks >= 70:
grade = "C"
elif marks >= 60:
grade = "D"
else:
grade = "F"
l3.config(text=f"Grade: {grade}")
b = Tk()
b.title("Grade Calculator")
b.geometry("300x200")
l1 = Label(b, text="Enter Marks:")
l1.pack()
e1 = Entry(b)
e1.pack()
b1 = Button(b, text="Check Grade", command=calculate_grade)
b1.pack()
l3 = Label(b, text="Grade: ")
l3.pack()
b.mainloop()
Explanation of Code
Step 1: Import Tkinter
We import Tkinter using from tkinter import *
, which allows us to create GUI components.
Step 2: Function to Calculate Grade
-
calculate_grade()
fetches the value from the entry field (e1.get()). - It converts the value to
float
(since marks may have decimals). - The
if-elif
conditions determine the grade based on marks:
- 90+ → A
- 80-89 → B
- 70-79 → C
- 60-69 → D
- Below 60 → F
The grade is displayed using l3.config(text=f"Grade: {grade}").
Step 3: Creating the Tkinter Window
- b = Tk() initializes the main application window.
- b.title("Grade Calculator") sets the window title.
- b.geometry("300x200") sets the window size.
Step 4: Adding Widgets
- Label (l1) – Displays "Enter Marks:".
- Entry (e1) – User types marks here.
- Button (b1) – Calls calculate_grade() when clicked.
- Result Label (l3) – Displays the grade after clicking the button.
Step 5: Running the Application
b.mainloop() keeps the GUI open and responsive.
Conclusion
This simple Tkinter project helps visualize how conditional statements work in Python. You can expand it by adding:
✅ GPA Calculator (average of multiple subjects).
✅ Color-coded Grades (Green for A, Red for F).
✅ Additional Features (Remarks like "Excellent", "Needs Improvement").
Try modifying it and share your version!