DEV Community

Blackmare01wolf
Blackmare01wolf

Posted on

πŸŽ“ Building a Simple Grade Calculator with Tkinter

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()

Enter fullscreen mode Exit fullscreen mode

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:
  1. 90+ β†’ A
  2. 80-89 β†’ B
  3. 70-79 β†’ C
  4. 60-69 β†’ D
  5. 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!

Top comments (1)

Collapse
 
blackmare01wolf profile image
Blackmare01wolf

I hope you found this guide helpful! πŸš€
πŸ’‘ What other Tkinter projects have you built?
πŸ’¬ Drop your thoughts and ideas in the comments