2017-04-24 2 views
0

제 친구와 저는 학교 프로젝트를 위해 숫자 추측 게임을하고 있지만, 우리는 Tkinter 프로그램으로 문제를 찾을 수 없습니다. 주된 목적은 컴퓨터가 1 ~ 100의 숫자를 생성하도록하고 사용자가 숫자를 추측해야합니다. 우리는 거의 대부분 끝났지 만 숫자를 추측 할 때마다 프로그램이 생성하는 숫자보다 추측 된 숫자가 더 낮을지라도 프로그램은 항상 "올라갑니다"를 반환합니다. 도와주세요!!Tkinter 숫자 추측 게임이 작동하지 않습니다.

이미 우리가 원하는 것을,하지만 우리의 프로그램 아래에 삽입 한

정말 우리 프로그램에서 무엇이 잘못되었는지 이해하려는 작업 프로그램을 발견했다

:

from Tkinter import * 
import random 
frame = Tk() 

frame.geometry("500x500") 
frame.title("guess the number") 

global ability 
ability = random.randint(1,100) 
print ability 

def retrieve_input(): 
    input = t1.get("1.0",'end-1c') 
    return input 

def startFunction(): 
    global t2 
    frame.destroy() 
    frame2 = Tk() 

    frame2.geometry("500x247") 
    frame2.title("guess the number") 

    l1 = Label(text="The Number Guessing Game", font=("Helvetica", 27)) 
    l1.place(x=10, y=10) 

    l2 = Label(text="Guess your number here (0-100)") 
    l2.place(x=10, y=100) 

    t1 = Text(width= 5, height= 1) 
    t1.place(x= 10, y= 124) 

    l3 = Label(text="Higher or Lower?") 
    l3.place(x=10, y=190) 

    t2 = Text(width= 15, height= 1) 
    t2.place(x= 10, y= 214) 

    b1 = Button(text="Enter", width= 5, height= 1, command= numberFunction) 
    b1.place(x= 10, y= 150) 

def numberFunction(): 
    global ability,t2 
    if input() == ability: 
      t2.insert(END,"correct") 
    if input() > ability: 
      t2.insert(END,"lower") 
    if input() < ability: 
      t2.insert(END,"higher") 

b1 = Button(text="START", width=12, height=2, command=startFunction) 
b1.place(x=210, y=210) 

frame.mainloop() 
+1

레이블 내가 얻을 :'는 EOFError를 : EOF를 line'에서'input() == ability :'를 읽었을 때. Tkinter 어플리케이션에서는 그런 식으로 입력을 사용할 수 없습니다. 당신이하고있는 일을 대신해서'Entry' 위젯을 만들고 사용해야합니다. – martineau

답변

0

은를 사용해보십시오 항목 위젯 :

첫째, 당신은 항목 위젯을 만들어야합니다

guess = Entry() 
guess.pack() 

위젯이 표시됩니다. 이제 사용자가 작성한 모든 것을 취하는 콜백이 필요합니다. 이것을 숫자 기능에 추가하십시오.

user_guess = int(guess.get()) 

이제 생성 된 임의의 숫자와 user_guess를 비교할 수 있습니다.

if user_guess == ability: 
     t2.insert(END,"correct") 
if user_guess > ability: 
     t2.insert(END,"lower") 
if user_guess < ability: 
     t2.insert(END,"higher") 

전체 코드는 다음과 같습니다

from tkinter import * 
import random 
frame = Tk() 

frame.geometry("500x500") 
frame.title("guess the number") 

global ability 
ability = random.randint(1,100) 
print (ability) 

def retrieve_input(): 
    input = t1.get("1.0",'end-1c') 
    return input 

def startFunction(): 
    global t2 
    frame.destroy() 
    frame2 = Tk() 

    frame2.geometry("500x247") 
    frame2.title("guess the number") 

    l1 = Label(text="The Number Guessing Game", font=("Helvetica", 27)) 
    l1.place(x=10, y=10) 

    l2 = Label(text="Guess your number here (0-100)") 
    l2.place(x=10, y=100) 

    t1 = Entry(width= 5) 
    t1.place(x= 10, y= 124) 

    l3 = Label(text="Higher or Lower?") 
    l3.place(x=10, y=190) 

    t2 = Entry(width= 15) 
    t2.place(x= 10, y= 214) 

    def numberFunction(): 
     global ability,t2 
     if int(t1.get()) == ability: 
      print("Correct") 
     if int(t1.get()) > ability: 
      print("Guess Lower") 
     if int(t1.get()) < ability: 
      print("Guess Higher") 

    b1 = Button(text="Enter", width= 5, height= 1, command= numberFunction) 
    b1.place(x= 10, y= 150) 



b1 = Button(text="START", width=12, height=2, command=startFunction) 
b1.place(x=210, y=210) 

frame.mainloop() 

대신 인쇄

은 높거나 낮은 추측 난 당신의 코드를 실행하려고하면 팩을하거나 장소

관련 문제