输入框出现问题,无法获得正确答案的入口

时间:2015-04-07 19:52:48

标签: tkinter random-sample

所以这个程序的目标是创建一个随机数游戏。它会要求您输入密码" Jump"一旦进入,就会打开游戏。游戏会向您显示4个随机数0-100,并要求您在输入框中按正确的顺序排列它们。我的问题是一切正常,除非我把有序数字放在输入框中,它总是错的。我需要制作4个输入框吗?这似乎是太多额外的工作。

from Tkinter import *
import random


def getvalue():
    password = ent.get()
    if password == "Jump":
        open_window2()
    else:
        root.destroy()



root = Tk()

lab = Label(root, text='Insert Password', fg="white",bg="blue")
ent = Entry(root, bg='white', fg="blue")
button = Button(root, text='Enter Password', command=getvalue)

ent.focus()

lab.grid(row=0)
ent.grid(row=0, column=1)
button.grid(row=2, column=1)



def open_window2():
    window2 = Tk()

    random_numbers = random.sample(range(100), 4)
    global getvalue2
    getvalue2 = IntVar(sorted(random_numbers))

    def correct_answer():
            user_input = ent2.get()
            print user_input

            """ I can get the lose window to open now, because the program is not
            accepting the correct answer and I can't figure out how to insert it correctly, make 4 input boxes"""
            if user_input == getvalue2:
                window3 = Tk()
                winlab1 = Label(window3, text="You have won! please select play again or exit")
                winbutton1 = Button(window3, text="Play Again", command=open_window2)
                winbutton2= Button(window3, text="Exit", command=window3.quit)

                winlab1.pack()
                winbutton1.pack()
                winbutton2.pack()

                window3.mainloop()

            else:
                window4 = Tk()
                loselab1 = Label(window4, text="You have lost :( please select play again or exit")
                losebutton1 = Button(window4, text="Play Again", command=open_window2)
                losebutton2 = Button(window4, text="Exit", command=window4.quit)
                loselab1.pack()
                losebutton1.pack()
                losebutton2.pack()

                window4.mainloop()



    lab2 = Label(window2, text="Welcome to the number game!", fg="white", bg="blue")
    lab3 = Label(window2, text=random_numbers,fg="white", bg="blue")
    lab4 = Label(window2, text="Please arrange the numbers in ascending order", fg="white", bg="blue")

    """look up how to create extra entry boxes in python"""
    ent2 = Entry(window2)
    global ent2




    button2 = Button(window2, text="submit", command = correct_answer)


    lab2.grid(row=0, column=0, sticky=W)
    lab3.grid(row=1, column=1)
    lab4.grid(row=1, column=0)
    ent2.grid(row=2, column=0)
    button2.grid(row=2, column=1)

    root.destroy()




root.mainloop()

2 个答案:

答案 0 :(得分:0)

这不符合您的期望:

if user_input == getvalue2:

尝试调试代码时,第一步是验证您对代码的假设。在这种情况下,您假设getvalue2是您在窗口小部件中输入的值。如果你打印出来,你会发现它不是你所期望的。

要从IntVar获取值,您必须使用get方法:

if user_input == getvalue2.get():

答案 1 :(得分:-2)

1)你没有使用python3.3所以请删除这个标签。

2)标签随机样本在这里也不相关。

3)您应该尝试在correct_answer函数中打印getvalue2,我相信您的问题是您没有将其声明为全局,或者user_input和getvalue2可能有不同的类型,因此您应该转换其中一个。