Average Marks Calculator in Python Tkinter

Filed Under: Python Modules
Avg Marks Python

Hey Coders! Today in this tutorial, we will understand how to build a GUI Average Marks Calculator using Tkinter. You need to input your subject and respective marks obtained out of 100 and then the generate button would calculate your percentage.

Whenever you get a result of any examination, the first thing you want to know is the percentage and grade that you have obtained and this application helps you to find that out.

Also Read: Python Tkinter tutorial – part 1

Implementing an Average Marks Calculator in Python Tkinter

Now that we have understood what the Average Marks Calculator is and how will it help you in daily life?. Now that you know the application let’s head over to the building the application.

Creating and Designing the Application Window

To begin working on the application, we first have to import tkinter module and assign our window as the variable app. We will also create some variables to customize our application window in the later section.

from tkinter import *

app = Tk()

Name_of_application = "Average Marks Calculator"
width_height = "600x450"
back_color = "red"

Now that we have the application window, we will start customizing the window. We will be changing the window’s title, using the title method. We can also determine the size of the application window using the geometry method. We can also prevent the window from resizing using the resizable method. We will also change the background color of the window in Tkinter using configure(bg=”) method.

app.title(Name_of_application)
app.geometry(width_height)
app.resizable(False, False)
app.configure(bg=back_color)

We will be adding the following widgets to our application:

  1. Label
  2. Entry
  3. Button
# Labels  
Title = Label(text="Welcome to Average Marks Calculator!",bg="black",
              fg="white",font=("Helvetica", 14))
Title.place(x=30, y=3)

Description_Text = """You need to input your subject and respective marks obtained out of 
100 and then the generate button would calculate your percentage and grade."""

Description = Label(text=Description_Text,bg="#FFC300",
                    fg="black",font=("Helvetica", 10)).place(x=30, y=40)
info =Label(text="Subject Name",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=90)
entry=Label(text="Marks Obtained",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=180, y=90)
subject=Label(text="Physics",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=130)
entry1 = Entry(app)
entry1.place(x = 180, y = 130)
subject=Label(text="Mathematics",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=150)
entry2 = Entry(app)
entry2.place(x = 180, y = 150)
subject=Label(text="Chemistry",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=170)
entry3 = Entry(app)
entry3.place(x = 180, y = 170)
subject=Label(text="Biology",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=190)
entry4 = Entry(app)
entry4.place(x = 180, y = 190)
subject=Label(text="English",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=210)
entry5 = Entry(app)
entry5.place(x = 180, y = 210)
subject=Label(text="3rd language",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=230)
entry6 = Entry(app)
entry6.place(x = 180, y = 230)
subject=Label(text="Social Science",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=250)
entry7 = Entry(app)
entry7.place(x = 180, y = 250)
b = Button(app, text = "Submit")
b.place(x=120,y=290)
Percentage=Label(text="PERCENTAGE",bg="black",
                fg="white",font=("Helvetica",10)).place(x=30,y=330)
entry8 = Entry(app,textvariable=m8,state='disabled')
entry8.place(x = 150, y = 330)
app.mainloop()
Average Marks Calculator Screen 2
Average Marks Calculator Screen 2

Adding Text Variables for the Entry Boxes

We will be adding 7 variables for the seven entry boxes in the application using the code below. We will be adding the variable to the text variable attribute of the respective entry boxes.

m1=StringVar()
m2=StringVar()
m3=StringVar()
m4=StringVar()
m5=StringVar()
m6=StringVar()
m7=StringVar()
m8=StringVar()
m1=StringVar()
m2=StringVar()
m3=StringVar()
m4=StringVar()
m5=StringVar()
m6=StringVar()
m7=StringVar()
m8=StringVar()
info =Label(text="Subject Name",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=90)
entry=Label(text="Marks Obtained",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=180, y=90)
subject=Label(text="Physics",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=130)
entry1 = Entry(app,textvariable=m1)
entry1.place(x = 180, y = 130)
subject=Label(text="Mathematics",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=150)
entry2 = Entry(app,textvariable=m2)
entry2.place(x = 180, y = 150)
subject=Label(text="Chemistry",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=170)
entry3 = Entry(app,textvariable=m3)
entry3.place(x = 180, y = 170)
subject=Label(text="Biology",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=190)
entry4 = Entry(app,textvariable=m4)
entry4.place(x = 180, y = 190)
subject=Label(text="English",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=210)
entry5 = Entry(app,textvariable=m5)
entry5.place(x = 180, y = 210)
subject=Label(text="3rd language",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=230)
entry6 = Entry(app,textvariable=m6)
entry6.place(x = 180, y = 230)
subject=Label(text="Social Science",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=250)
entry7 = Entry(app,textvariable=m7)
entry7.place(x = 180, y = 250)
b = Button(app, text = "Submit")
b.place(x=120,y=290)
Percentage=Label(text="PERCENTAGE",bg="black",
                fg="white",font=("Helvetica",10)).place(x=30,y=330)
entry8 = Entry(app,textvariable=m8,state='disabled')
entry8.place(x = 150, y = 330)
app.mainloop()

Adding Button Functionality

We need a function to compute the percentage of marks entered by the student. The code is below which takes the inputs from all the entry boxes and computes the percentage. The output is put in the last entry box which is later on disabled.

def marks():
    a=int(entry1.get())
    b=int(entry2.get())
    c=int(entry3.get())
    d=int(entry4.get())
    e=int(entry5.get())
    f=int(entry6.get())
    g=int(entry7.get())
    perc=((a+b+c+d+e+f+g)/7)
    entry8.config(state='normal')
    entry8.delete(0,END)
    entry8.insert(0,str(perc))
    entry8.config(state='disabled')

The Complete Tkinter Code for Implementing Average Marks Calculator

from tkinter import *

app = Tk()

Name_of_application = "Average Marks Calculator"
width_height = "600x450"
back_color = "red"

def marks():
    a=int(entry1.get())
    b=int(entry2.get())
    c=int(entry3.get())
    d=int(entry4.get())
    e=int(entry5.get())
    f=int(entry6.get())
    g=int(entry7.get())
    perc=((a+b+c+d+e+f+g)/7)
    entry8.config(state='normal')
    entry8.delete(0,END)
    entry8.insert(0,str(perc))
    entry8.config(state='disabled')

app.title(Name_of_application)
app.geometry(width_height)
app.resizable(False, False)
app.configure(bg=back_color)

# Labels  
Title = Label(text="Welcome to Average Marks Calculator!",bg="black",
              fg="white",font=("Helvetica", 14))
Title.place(x=30, y=3)

Description_Text = """You need to input your subject and respective marks obtained out of 
100 and then the generate button would calculate your percentage and grade."""

Description = Label(text=Description_Text,bg="#FFC300",
                    fg="black",font=("Helvetica", 10)).place(x=30, y=40)


m1=StringVar()
m2=StringVar()
m3=StringVar()
m4=StringVar()
m5=StringVar()
m6=StringVar()
m7=StringVar()
m8=StringVar()
info =Label(text="Subject Name",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=90)
entry=Label(text="Marks Obtained",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=180, y=90)
subject=Label(text="Physics",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=130)
entry1 = Entry(app,textvariable=m1)
entry1.place(x = 180, y = 130)
subject=Label(text="Mathematics",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=150)
entry2 = Entry(app,textvariable=m2)
entry2.place(x = 180, y = 150)
subject=Label(text="Chemistry",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=170)
entry3 = Entry(app,textvariable=m3)
entry3.place(x = 180, y = 170)
subject=Label(text="Biology",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=190)
entry4 = Entry(app,textvariable=m4)
entry4.place(x = 180, y = 190)
subject=Label(text="English",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=210)
entry5 = Entry(app,textvariable=m5)
entry5.place(x = 180, y = 210)
subject=Label(text="3rd language",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=230)
entry6 = Entry(app,textvariable=m6)
entry6.place(x = 180, y = 230)
subject=Label(text="Social Science",bg="red",
              fg="white",font=("Helvetica", 10)).place(x=10, y=250)
entry7 = Entry(app,textvariable=m7)
entry7.place(x = 180, y = 250)
b = Button(app, text = "Submit",command=marks)
b.place(x=120,y=290)
Percentage=Label(text="PERCENTAGE",bg="black",
                fg="white",font=("Helvetica",10)).place(x=30,y=330)
entry8 = Entry(app,textvariable=m8,state='disabled')
entry8.place(x = 150, y = 330)
app.mainloop()

A Sample Output

Average Marks Calculator Sample Output
Average Marks Calculator Sample Output

Conclusion

Congratulations! You now can develop your own average marks computer which you can use on a daily basis to know your percentage and keep your performance up to mark!

Thank you for reading!

Also Read: How To Create A GUI Age Calculator in Python Tkinter

close
Generic selectors
Exact matches only
Search in title
Search in content