2016-06-13 3 views
-1

저는 파이썬에 매우 익숙하며 특정 시험을 치르기 위해 최종 시험에 필요한 점수를 계산하는 tkinter를 사용하여 GUI를 만들려고합니다.NameError : name 's2gradefloat'이 정의되지 않았습니다.

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() 

을하지만 오류가 계속 :

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 

을 나는 아마 여기에 많은 실수를 만들고있어 실현 이것은 내가 지금까지 가지고있는 것입니다. 누구든지 도와 줄 수 있습니까? 귀하의 계산 기능에

+0

당신은 심지어's2gradefloat'이없는 보여 주었다 코드입니다. 's2grade = float (s2gradef.get())'이라는 줄이's2gradef'를 사전으로 액세스하려고 시도하는'crscore' 안에 있습니다. 그러나 당신은 결코 그런 식으로 선언하지 않았습니다. 그래서 당신은 그것을 수정하기를 원할지도 모릅니다. – idjaw

+0

@ idjaw 프로그래밍에 익숙하지 않은 이유가 무엇인지 알 수 있습니까? –

+0

's2gradef'는 무엇이되어야 하는가? 당신은 그것으로 무언가를하려하고 있습니다, 그래서 여러분은 거기에 어떤 종류의 데이터가 있다고 생각해야합니다. 's2gradef'에 무엇이 있어야합니까? – idjaw

답변

0

: 기본적으로

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

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

s2gradefloat is not defined --- 

, 당신은 내가 asuming하고 사용자의 입력 상자에서 값을 얻기 위해 노력하고있다?

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

그래서 다시 다음

s2grade.get()하지만를 사용한다 ---이 다른 오류로 이어질 것입니다 - 당신의 변수를 할당하기 전에 참조되고있다.

또 다른 빠른 참고 :

당신입니다 당신의 계산 함수 내에서 로컬 변수 이름을 사용하여 재 및 그 밖에,이

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

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 
좋은 방법이 아닙니다

변수를 고유하게 유지하면 많은 문제를 방지 할 수 있습니다. 예를 들어

:

#------------------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 
+0

그래,이 방정식에서 사용할 수 있도록 입력 상자에 입력 된 숫자를 부동으로 변환하려고합니다. rscore = (100 * dgrade - (100 - 50) * s2grade)/50 –

+0

당신의 변환이 좋다. s2grade, idk 대신에 새로운 변수 이름을 사용하고 'e'를 제거하고 s2grad = float (s2grade.get()) – glls

+0

을 로컬 변수에 대한 rscore 변수 – glls

관련 문제