So I made this code that calculates a student's average and I am trying to get the results to appear at the bottom of the window. the problem is that I want a new result label to appear every time I click on the "Submit" button. right now it's only replacing the existing label with the new data.
import tkinter as tk
def calculate_status(average):
if 0 <= average <= 39:
return "Failed"
elif 40 <= average <= 55:
return "Passed"
elif 56 <= average <= 70:
return "Great"
elif 71 <= average <= 100:
return "Distinctive"
def grade(name, grades):
average = (sum(grades) / len(grades))
status = calculate_status(average)
result_label = tk.Label(window, text=f"STUDENT NAME: {name}\nAVERAGE: {average}\nSTATUS: {status}", wraplength=300)
result_label.grid(row=3, columnspan=2, padx=5, pady=5)
last_row += 8
def get_grades():
grade_text = input_grade_entry.get()
grades = [float(grade) for grade in grade_text.split(",")]
for grade in grades:
if not 0 <= grade <= 100:
input_grade_entry.delete(0, tk.END)
input_grade_entry.insert(0, "Grades must be between 0 and 100 separated by commas.")
return []
return grades
def get_student_info():
global last_row
name = name_entry.get()
grades = get_grades()
if grades:
grade(name, grades)
# Create the main window
window = tk.Tk()
window.title("Student Grades")
# Create and place labels and entry widgets
tk.Label(window, text="Student Name:").grid(row=0, column=0, padx=5, pady=5, sticky="e")
name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1, padx=5, pady=5)
tk.Label(window, text="Grades (comma-separated):").grid(row=1, column=0, padx=5, pady=5, sticky="e")
input_grade_entry = tk.Entry(window)
input_grade_entry.grid(row=1, column=1, padx=5, pady=5)
# Create and place the submit button
submit_button = tk.Button(window, text="Submit", command=get_student_info)
submit_button.grid(row=2, columnspan=2, padx=5, pady=5)
# Initialize last row
last_row = 3
# Start the GUI event loop
window.mainloop()
I don't really know, that's why I came here...
Explanation given as and where I've made changes in the code.