Python Tkinter数字猜测游戏不起作用

Python Tkinter数字猜测游戏不起作用,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我和我的朋友正在为一个学校项目做一个数字猜谜游戏,但不管怎样,我们似乎找不到Tkinter程序的问题。主要目标是让计算机生成1-100之间的数字,用户必须猜测数字。我们几乎完成了大部分工作,但每当我们猜测数字时,程序总是返回“更高”,即使我们猜测的数字低于程序生成的数字。请帮忙 我已经找到了一个可以满足我们需要的工作程序,但我真的很想了解我们的程序出了什么问题 我在下面插入了我们的程序: from Tkinter import * import random frame = Tk() fram

我和我的朋友正在为一个学校项目做一个数字猜谜游戏,但不管怎样,我们似乎找不到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()
从Tkinter导入*
随机输入
frame=Tk()
框架几何(“500x500”)
框架标题(“猜数字”)
全球能力
能力=random.randint(1100)
印刷能力
def检索_输入():
输入=t1.get(“1.0”,“end-1c”)
返回输入
def startFunction():
全局t2
frame.destroy()
frame2=Tk()
框架2.几何结构(“500x247”)
框架2.标题(“猜数字”)
l1=标签(text=“猜数字游戏”,font=(“Helvetica”,27))
l1.位置(x=10,y=10)
l2=标签(text=“在这里猜你的号码(0-100)”)
l2.位置(x=10,y=100)
t1=文本(宽度=5,高度=1)
t1.地点(x=10,y=124)
l3=标签(text=“较高还是较低?”)
l3.位置(x=10,y=190)
t2=文本(宽度=15,高度=1)
t2.位置(x=10,y=214)
b1=按钮(text=“Enter”,宽度=5,高度=1,命令=numberFunction)
b1.位置(x=10,y=150)
def numberFunction():
全球能力,t2
如果输入()==能力:
t2.插入(结束,“正确”)
如果输入()>能力:
t2.插入(末端,“下部”)
如果输入()<能力:
t2.插入(结束,“较高”)
b1=按钮(text=“开始”,宽度=12,高度=2,命令=startFunction)
b1.位置(x=210,y=210)
frame.mainloop()

尝试使用条目小部件:

首先,您需要创建一个条目小部件:

guess = Entry()
guess.pack()
这将显示一个条目小部件。 现在,我们需要一个回调函数,它接受用户编写的任何内容。将此添加到您的NumberFunction中

user_guess = int(guess.get())
现在,您可以将用户的猜测与生成的随机数进行比较

if user_guess == ability:
        t2.insert(END,"correct")
if user_guess > ability:
        t2.insert(END,"lower")
if user_guess < ability:
        t2.insert(END,"higher")
如果用户猜==能力:
t2.插入(结束,“正确”)
如果用户_guess>能力:
t2.插入(末端,“下部”)
如果用户猜测<能力:
t2.插入(结束,“较高”)
完整的代码是:

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()
从tkinter导入*
随机输入
frame=Tk()
框架几何(“500x500”)
框架标题(“猜数字”)
全球能力
能力=random.randint(1100)
印刷(能力)
def检索_输入():
输入=t1.get(“1.0”,“end-1c”)
返回输入
def startFunction():
全局t2
frame.destroy()
frame2=Tk()
框架2.几何结构(“500x247”)
框架2.标题(“猜数字”)
l1=标签(text=“猜数字游戏”,font=(“Helvetica”,27))
l1.位置(x=10,y=10)
l2=标签(text=“在这里猜你的号码(0-100)”)
l2.位置(x=10,y=100)
t1=入口(宽度=5)
t1.地点(x=10,y=124)
l3=标签(text=“较高还是较低?”)
l3.位置(x=10,y=190)
t2=入口(宽度=15)
t2.位置(x=10,y=214)
def numberFunction():
全球能力,t2
如果int(t1.get())==能力:
打印(“正确”)
如果int(t1.get())>能力:
打印(“猜低”)
如果int(t1.get())<能力:
打印(“猜测更高”)
b1=按钮(text=“Enter”,宽度=5,高度=1,命令=numberFunction)
b1.位置(x=10,y=150)
b1=按钮(text=“开始”,宽度=12,高度=2,命令=startFunction)
b1.位置(x=210,y=210)
frame.mainloop()

当我尝试运行您的代码时,您可以打包或放置标签,而不是打印更高或更低的猜测,我得到:
eoferor:EOF当读取行上的一行时
if input()==ability:
。在Tkinter应用程序中,您不能以这种方式获取use输入。对于您正在做的事情,您需要创建并使用
条目
小部件。