how to add a new label every time I click on a button

60 Views Asked by At

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...

2

There are 2 best solutions below

0
Suramuthu R On

Explanation given as and where I've made changes in the code.

import tkinter as tk

#Declare a variable count here.
count = 0

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):
    global last_row, count
    average = (sum(grades) / len(grades)) 
    status = calculate_status(average)
    
    # Dont declare a variable name for the label. row value should be multiple of count
    tk.Label(window, text=f"STUDENT NAME: {name}\nAVERAGE: {average}\nSTATUS: {status}", wraplength=300).grid(row=2 + (count*4), 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():
    
    # Count every click and make the variable count global

    global last_row, count
    count = count + 1
    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()
1
acw1668 On

You have already created new label for each result, but you put them in the same position (row=3) so newly created label will overlap the previous one.

Suggest to create a frame for the result labels and use .pack() on those labels instead:

...
def grade(name, grades):
    average = (sum(grades) / len(grades))
    status = calculate_status(average)
    # create label as child of frame
    result_label = tk.Label(frame, text=f"STUDENT NAME: {name}\nAVERAGE: {average}\nSTATUS: {status}", wraplength=300)
    # simply use .pack()
    result_label.pack(padx=5, pady=5)
...

# create a frame for the result labels
frame = tk.Frame(window)
frame.grid(row=3, columnspan=2)

# Start the GUI event loop
window.mainloop()