Search code examples
pythontkintertkinter-entry

How to use Entry widgets to display output?


I am building a simple program using widgets that will show someone's grade based on their score. E.G. 5 = A, 4 = B ... 1 & 0 = F.

One problem I am facing with my program is displaying my result in a label after inputting a score in an entry box. When I originally ran my program, it displayed the grade based on the score, but not from an Entry box.

I've you-tubed countless videos involving entry boxes, but none of them worked. I also tried setting the grade ("grade") equal to my entry box (entry).

Here is my program:

'''Program to connect entry to button and output results'''

import tkinter as tk

constant = tk.Tk()

def put_grade():

    entry = tk.Entry(constant)
    entry.place(relx = .4, rely = .1, relwidth = .2, relheight = .1)

    values = [5, 4, 3, 2, 1, 0]
    grades = ["A", "B", "C", "D", "F", "F"]

    # Source of problem
    grade = entry

    done = False
    index = 0

    while not done:

        if values[index] == grade:

            grade = grades[index]
            done = True

        index += 1

    return grade

def get_grade(grade):

    label = tk.Label(constant, bg = "light blue", text = grade)
    label.place(relx = .4, rely = .2, relwidth = .2, relheight = .5)

def button_clicked():

    button = tk.Button(constant, bg = "light green", text = "Check grade", command = put_grade)
    button.place(relx = .4, rely = .6, relwidth = .2, relheight = .1)

def main():

    grade = put_grade()
    get_grade(grade)
    button_clicked()
    tk.mainloop()

if __name__ == '__main__':

    main()

If anyone needs any further clarifications on my code or my expected output, I would be honored to clarify.


Solution

  • Here's a somewhat modified version of your original code. You can fix the remaining logic problems.

    '''Program to connect entry to button and output results'''
    
    import tkinter as tk
    
    values = [5, 4, 3, 2, 1, 0]
    grades = ["A", "B", "C", "D", "F", "F"]
    
    root = tk.Tk()
    
    def put_grade(entry,label):
        grade=entry.get()
        try:
            grade=int(grade)
        except Exception as e:
            grade=0
    
        done = False  
        index = 0
        while not done:
            if values[index] == grade:
                grade = grades[index]
                done = True
            index += 1
    
        label.config(text=grade)
        
    def make_gui(root):
        entry = tk.Entry(root)
        entry.place(relx = .4, rely = .1, relwidth = .2, relheight = .1)
        label = tk.Label(root, bg = "light blue", text = "0")
        label.place(relx = .4, rely = .2, relwidth = .2, relheight = .5)
        button = tk.Button(root, bg = "light green", text = "Check grade", command = lambda:put_grade(entry,label))
        button.place(relx = .4, rely = .6, relwidth = .2, relheight = .1)
    
    def main():
        make_gui(root)
        tk.mainloop()
    
    if __name__ == '__main__':
        main()