2017-04-24 14 views
0

私の友人と私は学校プロジェクトのための数々の推測ゲームに取り組んでいますが、Tkinterプログラムで問題を見つけることはできません。主な目的は、コンピュータに1〜100の数値を生成させ、その数値を推測することです。ほとんど終了していますが、数字を推測するたびに、推測した数がプログラムの生成数よりも少なくても、プログラムは常に「上がります」と返します。助けてください!!Tkinter数推測ゲームが動作しない

私はすでに我々が何をしたいん作業プログラムを発見したが、私は本当に私たちのプログラム

に間違っていたかを理解したい、私は下に我々のプログラムを挿入した

:ANを使用してみてください

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

をパックや場所することができます入力行()==能力: 'の行に行'を読むとき。 Tkinterアプリケーションでは、そのように入力を使用することはできません。あなたがしていることに対して、代わりに 'Entry'ウィジェットを作成して使用する必要があります。 – martineau

答えて

0

エントリウィジェット:

まず、あなたはエントリウィジェットを作成する必要があります。

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

完全なコードは次のようになります。 `例外EOFError:EOF

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

代わりに、印刷の高い推測以下、私はあなたのコードを実行しようとすると、私が得るラベル

関連する問題