Tkinter号码猜测游戏无效

时间:2017-04-24 19:39:03

标签: python python-2.7 tkinter

我和我的朋友正在为一个学校项目开发一个数字猜谜游戏,但无论如何,我们似乎无法通过我们的Tkinter程序找到问题。主要目标是让计算机生成1-100的数字,用户必须猜测数字。我们几乎完成了大部分时间,但每当我们猜测数字时,程序总会返回"走高,"即使我们猜到的数字低于程序生成的数字。请帮忙!!

我已经找到了一个可以满足我们想要的工作程序,但我真的很想了解我们程序中出了什么问题

我在下面插入了我们的程序:

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 个答案:

答案 0 :(得分:0)

尝试使用条目小部件:

首先,您需要创建一个Entry Widget:

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

您可以打包或放置标签

,而不是打印猜测更高或更低
相关问题