tkinter

时间:2016-04-23 19:07:29

标签: python tkinter

我做了一个简单的猜测数字' Python中的游戏没有任何困难,所以我想挑战自己并更进一步。我试图在Tkinter中制作它,并添加猜测'和'计时器'变量

这是我的代码:

import tkinter
import random

guesses = 0
timeleft = 30

numbers = ['1','2','3','4','5','6','7','8','9','10']
numbers2 = []

doTick = True

def startGame(event):
    global doTick

    doTick = True

    number = random.choice(numbers)
    numbers.remove(number)
    numbers2.append(number)

    if timeleft == 30:
        countdown()

    wrong_guess()

def wrong_guess():

    global score
    global timeleft
    global guesses

    if timeleft > 0:
        e.focus_set()

        if int(e.get()) == int(numbers2[0]):
            guesses += 1
            guessesLabel.config(text="Guesses: " + str(guesses))
            correct_guess()
        else:
            if int(e.get()) > int(numbers2[0]):
                Label.config(text="Your guess is too high!")
                guesses += 1
                guessesLabel.config(text="Guesses: " + str(guesses))
                wrong_guess()
            else:
                Label.config(text="Your guess is too low!")
                guesses += 1
                guessesLabel.config(text="Guesses: " + str(guesses))
                wrong_guess()

        e.delete(0, tkinter.END)

def countdown():

    global timeleft

    if not doTick:
        return

    if timeleft > 0:
        timeleft -= 1
        timeLabel.config(text="Time left:" + str(timeleft))
        root.after(1000, countdown)

def correct_guess():

    global guesses
    global timeleft
    global doTick

    doTick = False

    guessesLabel.config(text="You correctly guessed the number in " + str(guesses) + " guesses!")

    timeLabel.config(text="You had " + str(timeleft) + " seconds to spare!")

root = tkinter.Tk()
root.title("Guess the Number")
root.state('zoomed')

timeLabel = tkinter.Label(root, text="", fg="red",font=('Helvetica',25))
timeLabel.pack()

guessesLabel = tkinter.Label(root,text="", fg="green",font=('Helvetica',25))
guessesLabel.pack()

Label = tkinter.Label(root, text="", fg="blue",font=('Helvetica',25))
Label.pack()

e = tkinter.Entry(root)
root.bind('<Return>',startGame)
e.pack()
e.focus_set()

root.mainloop()

我的问题是,当程序运行时,会出现以下错误消息:

RecursionError:调用Python对象时超出最大递归深度

我最好的猜测是语句有些问题,但我不确定这种情况发生在哪里以及如何解决这个问题。

对于那些提出要求的人来说,这是错误消息:

在win32上的Python 3.5.1(v3.5.1:37a07cee5969,2015年12月6日,01:38:48)[MSC v.1900 32位(英特尔)] 输入&#34; copyright&#34;,&#34; credit&#34;或&#34; license()&#34;了解更多信息  RESTART:C:\ Users \ Acer_PC \ MINCH \ Year 10 \ Computing \ python \猜测Tkinter回调中的number.py异常 Traceback(最近一次调用最后一次):   文件&#34; C:\ Users \ Acer_PC \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ tkinter__init __。py&#34;,第1549行,调用     return self.func(* args)

1 个答案:

答案 0 :(得分:2)

函数wrong_guess正在调用自身,这经常在终端程序中用来发出信号“回到函数的开头再试一次”但它不被认为是好的做法,除非等待,否则肯定不行输入内容的用户是该功能的一部分。 (正如你所目睹的那样)

考虑应该真正调用函数wrong_guess时,每次按下<Return>键(并且程序已经启动)时检查用户是否有意义

def press_enter_callback(e=None):
    if doTick:
        #game is running
        wrong_guess() #might be better to call it "check_guess"
    else:
        startGame(e) #if there is no countdown clock then start a new game

root.bind('<Return>',press_enter_callback)

然后删除wrong_guess中对wrong_guess的两次调用,因为它们毫无意义。

您的代码也缺少一些条件会重置计时器或在倒计时达到0时设置doTick = False