Importing necessary libraries
The first step in creating our GPA calculator is to import the necessary libraries. In this case, we will be using the Tkinter library to create the GUI and the messagebox library to display error messages.
import tkinter as tk
from tkinter import messagebox
Creating the “calculate_gpa” function
The next step is to create a function that will handle the calculation of the GPA. This function will be triggered when the user clicks the “Calculate GPA” button.
def calculate_gpa():
# Get the grades and credit hours from the entry widgets
grades = grade_entry.get()
credit_hours = credit_entry.get()
# Split the grades and credit hours into lists
grades = grades.split(',')
credit_hours = credit_hours.split(',')
# Convert the grades and credit hours to float
grades = [float(grade) for grade in grades]
credit_hours = [float(hour) for hour in credit_hours]
# Check that the number of grades and credit hours match
if len(grades) != len(credit_hours):
messagebox.showerror("Error", "The number of grades and credit hours must match.")
return
# Calculate the weighted average
weighted_sum = 0
total_hours = 0
for i in range(len(grades)):
weighted_sum += grades[i] * credit_hours[i]
total_hours += credit_hours[i]
# Calculate the GPA
gpa = weighted_sum / total_hours
# Display the GPA
gpa_label.config(text=f"GPA: {gpa:.2f}")
This function first gets the input from the entry widgets, performs the necessary calculation, and displays the result in the gpa_label widget.
Creating the window and widgets
The next step is to create the window and the necessary widgets. We create a new window using the Tk() class, and set the title to “GPA Calculator”. We also create labels and entry widgets for the user to input their grades and credit hours, a button to trigger the calculation, and a label to display the result.
# Create a new window
root = tk.Tk()
root.title("GPA Calculator")
# Create labels and entry widgets for the grades and credit hours
grade_label = tk.Label(root, text="Grades (comma separated):")
grade_entry = tk.Entry(root)
credit_label = tk.Label(root, text="Credit hours (comma separated):")
credit_entry = tk.Entry(root)
# Create a button to trigger the GPA calculation
calculate_button = tk.Button(root, text="Calculate GPA", command=calculate_gpa)
#Create a label to display the GPA
gpa_label = tk.Label(root, text="")
Organizing the layout:
The layout of the widgets is organized using the grid method, which allows for precise positioning of the widgets within the window.
# Organize the layout using the grid method
grade_label.grid(row=0, column=0)
grade_entry.grid(row=0, column=1)
credit_label.grid(row=1, column=0)
credit_entry.grid(row=1, column=1)
calculate_button.grid(row=2, column=0, columnspan=2)
gpa_label.grid(row=3, column=0, columnspan=2)
Combine Code
import tkinter as tk
from tkinter import messagebox
def calculate_gpa():
# Get the grades and credit hours from the entry widgets
grades = grade_entry.get()
credit_hours = credit_entry.get()
# Split the grades and credit hours into lists
grades = grades.split(',')
credit_hours = credit_hours.split(',')
# Convert the grades and credit hours to float
grades = [float(grade) for grade in grades]
credit_hours = [float(hour) for hour in credit_hours]
# Check that the number of grades and credit hours match
if len(grades) != len(credit_hours):
messagebox.showerror("Error", "The number of grades and credit hours must match.")
return
# Calculate the weighted average
weighted_sum = 0
total_hours = 0
for i in range(len(grades)):
weighted_sum += grades[i] * credit_hours[i]
total_hours += credit_hours[i]
# Calculate the GPA
gpa = weighted_sum / total_hours
# Display the GPA
gpa_label.config(text=f"GPA: {gpa:.2f}")
# Create a new window
root = tk.Tk()
root.title("GPA Calculator")
# Create labels and entry widgets for the grades and credit hours
grade_label = tk.Label(root, text="Grades (comma separated):")
grade_entry = tk.Entry(root)
credit_label = tk.Label(root, text="Credit hours (comma separated):")
credit_entry = tk.Entry(root)
# Create a button to trigger the GPA calculation
calculate_button = tk.Button(root, text="Calculate GPA", command=calculate_gpa)
# Create a label to display the GPA
gpa_label = tk.Label(root, text="")
# Organize the layout using the grid method
grade_label.grid(row=0, column=0)
grade_entry.grid(row=0, column=1)
credit_label.grid(row=1, column=0)
credit_entry.grid(row=1, column=1)
calculate_button.grid(row=2, column=0, columnspan=2)
gpa_label.grid(row=3, column=0, columnspan=2)
root.mainloop()
Running the program
Finally, we run the program using the root.mainloop()
function which will keep the program running and waiting for user input until the window is closed.
Output Simple GPA

Conclusion
This is a basic example of how to create a GPA calculator using Tkinter. There are many ways to improve and expand upon this program, such as adding error handling, user input validation, and additional features. With the knowledge gained from this tutorial, you can start creating your own Tkinter based applications.
In addition to this tutorial, you can also find more information on creating a GPA calculator using Tkinter in Python by visiting the following external resources:
- The official Tkinter documentation: https://docs.python.org/3/library/tk.html
- A tutorial on creating a GPA calculator with Tkinter on the Python Mega Widgets website: https://pymw.github.io/pmw2/doc/contents/gpa.html
- A YouTube video tutorial on creating a GPA calculator using Tkinter: https://www.youtube.com/watch?v=xWxFJ7HdGc4
Please note that the external links may not be up-to-date and may not be related to the current context of the application.