0

I am working on a python Tkinter assignment in which an input box accepts user input and then displays the GPA and student status. Im pretty sure I have this setup wrong and am looking for advice on how to use an If statement to determine student status. Should everything fall under the getStudentStatus function or is it better to have two functions?

import math
from tkinter import *

#calculate student status
def getStudentStatus():
data = float(inputGPA.get())
floatedGPA = (data)
#Print student status
label2 = Label(GPA, text = 'Your GPA is:' ' %.2f' %     floatedGPA).grid(row = 3, column = 0)
label3 = Label(GPA, getStanding).grid(row = 4, column = 0)
return

def getStanding():
#Get academic Status
if(data>=3.5):
    return("This student is on the Deans List")
elif(data>=2.0):
    return("This student is on Acedmic Probation")
else:
    return("This student has Regular Standing")


#define window
GPA = Tk()
GPA.title("GPA Tool")
GPA.geometry("200x200")

#define variables
inputGPA = StringVar()

#label for text box
label1 = Label(GPA, text="Enter your GPA: ").grid(row = 0, column = 0)

#entry text box
entry1 = Entry(GPA, textvariable = inputGPA).grid(row = 1, column = 0)

#submit button
button1 = Button(GPA, text="Submit", command=getStudentStatus).grid(row = 2, column = 0)

GPA.mainloop()
  • Is the `getStudentStatus()` supposed to return anything? Because I see an empty return statement. – tygzy Mar 28 '20 at 19:50
  • Actually that wasn't meant to be there. I forgot to remove it. – subtlebliss Mar 28 '20 at 19:53
  • @subtlebliss You are stacking widgets, on every click `Button(..., text="Submit"` you create new `Label(...`. Read also [AttributeError: NoneType object has no attribute ...](https://stackoverflow.com/a/1101765/7414759) – stovfl Mar 28 '20 at 20:53

1 Answers1

1

To avoid that a new label is created on every button press, you could create an empty label outside of the function.

To assign the label object to a variable, split the creation and the placement.

In the function you could configure the label and add the wanted output.

Additionally you need to pass the float to the second function so that the comparison operators have a value to compare.

import math
from tkinter import *

#calculate student status
def getStudentStatus():
    data = float(inputGPA.get())
    floatedGPA = (data)
    #Print student status
    label2.configure(text = 'Your GPA is:' ' %.2f' %     floatedGPA)
    label3.configure(text = getStanding(floatedGPA))

def getStanding(data):
    #Get academic Status
    if(data>=3.5):
        return("This student is on the Deans List")
    elif(data>=2.0):
        return("This student is on Acedmic Probation")
    else:
        return("This student has Regular Standing")


#define window
GPA = Tk()
GPA.title("GPA Tool")
GPA.geometry("200x200")

#define variables
inputGPA = StringVar()

#label for text box
label1 = Label(GPA, text="Enter your GPA: ").grid(row = 0, column = 0)

#entry text box
entry1 = Entry(GPA, textvariable = inputGPA).grid(row = 1, column = 0)

# create empty labels
label2 = Label(GPA, text = '')
label3 = Label(GPA, text='')

# place empty labels
label2.grid(row = 3, column = 0)
label3.grid(row = 4, column = 0)

#submit button
button1 = Button(GPA, text="Submit", command=getStudentStatus).grid(row = 2, column = 0)

GPA.mainloop()
Thomas H.
  • 49
  • 4
  • Thank you for your response. I now can see what I was missing as getStanding didn't know where to pull the data from. – subtlebliss Mar 28 '20 at 22:52