Neill B Neill B - 5 months ago 15
Python Question

NameError: name 's2gradefloat' is not defined

I'm very new to python and i'm trying to create a GUI using tkinter that calculates the score you need on your final exam to get a certain grade. This is what I have so far:

from tkinter import *

root = Tk()
root.geometry("600x550+300+50")
root.title("")
backgroundcolor = '#%02x%02x%02x' %(0,0,0)
root.config(background= backgroundcolor)
#------------------Calculation----------------------#
def crscore():
s2grade = float(s2gradef.get())
dgrade = float(dgradef.get())
rscore = (100*dgrade - (100 - 50)* s2grade ) / 50
rscorelabel=Label(root,text="Required Test Score: %s" %rscore).place(x=250,y=370)
return
#------------------Labels----------------------#
Label1=Label(root,
text='Final Exam Calclulator',
fg='white',
bg='black',
font= "a 17 bold").place(x=170,y=20)
Label3=Label(root,
text='Enter Current Semester 2 Grade',
fg='white',
bg='black',
font= "a 12 bold").place(x=100,y=100)

Label4=Label(root,
text='Enter Desired Final Grade',
fg='white',
bg='black',
font= "a 12 bold").place(x=100,y=165)
#------------------EntryBoxes----------------------#
s2grade = StringVar()
dgrade = StringVar()
s2grade = Entry(root,textvariable=s2grade).place(x=395,y=100)
dgrade = Entry(root,textvariable=dgrade).place(x=360,y=165)
#------------------Button----------------------#
button1=Button(root,text='Calculate Required Score',command=crscore).place(x=250,y=240)
root.mainloop()


But keep getting the errors:

Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__
return self.func(*args)
File "E:/FinalProject.py", line 10, in crscore
s2grade = float(s2gradefloat.get())
NameError: name 's2gradefloat' is not defined


I realize i'm probably making many mistakes here. Can anyone help?

Answer

in your calculation function:

#------------------Calculation----------------------#

def crscore():
    s2grade = float(s2gradef.get())
    dgrade = float(dgradef.get())

s2gradefloat is not defined --- 

basically, you are trying to get the value from your entry box I am asuming?

s2grade = Entry(root,textvariable=s2grade).place(x=395,y=100)

so you should be using s2grade.get()

but then again--- this will lead to another error - your variable is being referenced before assignment.

Another quick note:

you are re-using variable names locally, within your calculation function and outside of it, this is not a good practice

s2grade = StringVar()
dgrade = StringVar()
s2grade = Entry(root,textvariable=s2grade).place(x=395,y=100)
dgrade = Entry(root,textvariable=dgrade).place(x=360,y=165)

AND

def crscore():
    s2grade = float(s2gradef.get())
    dgrade = float(dgradef.get())
    rscore = (100*dgrade - (100 - 50)* s2grade ) / 50
    rscorelabel=Label(root,text="Required Test Score: %s" %rscore).place(x=250,y=370)
    return

Try and keep your variables unique, this will save you a lot of trouble.

For Ex:

#------------------Calculation----------------------#

def crscore():
    s2grad = float(s2grade.get())
    dgrad = float(dgrade.get())
    print(s2grad)
    print(dgrad)
    rscore = (100*dgrad - (100 - 50)* s2grad ) / 50
    rscorelabel=Label(root,text="Required Test Score: %s" %rscore).place(x=250,y=370)
    return
#------------------EntryBoxes----------------------#
s2grade = StringVar()
dgrade = StringVar()
Entry(root, textvariable=s2grade).place(x=395, y=100)
Entry(root, textvariable=dgrade).place(x=360, y=165)
#removed assigned variable names to the Entry widgets, as they are not being
#used " There's no point in assigning variables to 
#Entry(...).place(...) always returns None" as mentioned by @Brian Oakley
Comments