2017-04-24 160 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當在線閱讀一行'if input()== ability:'。您無法在Tkinter應用程序中使用這種輸入方式。對於你在做什麼,你將需要創建和使用'Entry'小部件。 – martineau

回答

0

嘗試使用進入小部件:

首先,你需要創建一個Entry控件:

guess = Entry() 
guess.pack() 

這將有一個入口小部件顯示。 現在,我們需要有一個回調,無論用戶寫什麼。將此添加到您的NumberFunction中

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

代替打印的猜測更高或更低,可以包或者將標籤放置