2017-01-30 1 views
0

나는 init 함수를 가지고 tkinter 창을 만든다. 내부 창에는 곱셈 5 * 10과 입력 상자가 있습니다. 사용자가 정답으로 상자를 채우면 결과 함수를 실행하는 버튼을 누른 후 올바른 메시지로 레이블을 표시해야하며 그렇지 않으면 잘못된 메시지로 레이블을 표시해야합니다. 문제는 다음과 같습니다.파이썬 Tkinter 엔트리 - 값을 얻는다

심지어 대답이 맞으면 잘못된 메시지가 표시됩니다. 결과 함수에서 self.content를 출력하면 터미널에 올바른 값이 표시됩니다. 문제는 결과 함수의 if 문 안에 있습니다. Tkinter를 가져 오기 *에서

클래스 소품 :

def __init__(self): 

    self.root = Tk() 
    self.root.geometry("800x600") 

    self.x = 5 
    self.y = 10 

    self.title = Label(self.root, text = "TEST") 
    self.title.grid(row=0, column=0, columnspan=6) 
    self.title.config(font=("Courier", 30)) 

    self.labelx = Label(self.root,text=self.x) 
    self.labelx.grid(row=2, column=2) 
    self.labelx.config(font=("Courier", 30)) 

    self.epi = Label(self.root,text="X") 
    self.epi.grid(row=2, column=3) 
    self.epi.config(font=("Courier", 30)) 

    self.labely = Label(self.root,text=self.y) 
    self.labely.grid(row=2, column=4) 
    self.labely.config(font=("Courier", 30)) 

    self.total = Entry(self.root, font = "Courier 25 bold",justify="center",width=3) 
    self.total.grid(row=2, column=5, padx=20) 

    button = Button(self.root, text="OK", command = self.result) 
    button.grid(row=3, column=2) 

    self.content = Entry.get(self.total) 

    self.root.mainloop() 

def result(self): 
    if self.content == (self.x * self.y): 
     self.labres = Label(self.root, text="Right") 
     self.labres.grid(row=2, column=6) 
     self.labres.config(font=("Courier", 30)) 
    else: 
     self.labres = Label(self.root, text="Wrong") 
     self.labres.grid(row=2, column=6) 
     self.labres.config(font=("Courier", 30)) 

시작 = 소품() 값이 입력되기 전에이 항목의 내용을 읽고

답변

1

. 버튼을 클릭 한 후에 내용을 읽어야합니다.

또한 문자열을 self.content == (self.x * self.y)의 int와 비교합니다. int(self.content) == (self.x * self.y)을 사용해야합니다.

업데이트 된 결과 기능은 다음과 같습니다

def result(self): 
     self.content = Entry.get(self.total) 
     if int(self.content) == (self.x * self.y): 
      self.labres = Label(self.root, text="Right") 
      self.labres.grid(row=2, column=6) 
      self.labres.config(font=("Courier", 30)) 
     else: 
      self.labres = Label(self.root, text="Wrong") 
      self.labres.grid(row=2, column=6) 
      self.labres.config(font=("Courier", 30)) 
+0

경우 내가 수행하는 경우 INT (self.content) 내가 얻을 : ValueError를이 : 기본 10 INT()에 대한 유효하지 않은 문자 : – Athon

+0

@Athon 확인'자아를 확인하세요 .content'는 비어 있지 않거나 알파벳이나 기호를 포함하지 않습니다. 'int (self.content)'에서 사용하기 전에'self.content'를 검증해야합니다. –

+0

전에 당신이 유효성 검사를한다는 의미를 설명해 주시겠습니까? – Athon