python 海龟程序给出“Tkinter 回调中出现异常”错误

问题描述 投票:0回答:1

我正在尝试编写一个两只乌龟追逐另一只乌龟的Python程序。然而代码在游戏几秒钟后崩溃了,给出了“Tkinter 回调中的异常”

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
    return self.func(*args)
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 806, in callit
    func(*args)
  File "D:\ZC-CSCI 101\pythonProject\myturtle.py", line 41, in follow_runner
    follow.setheading(follow.towards(run))
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 1937, in setheading
    self._rotate(angle)
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3279, in _rotate
    self._update()
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2662, in _update
    self._drawturtle()
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3009, in _drawturtle
    shape = self._polytrafo(self._getshapepoly(tshape))
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 2961, in _polytrafo
    e0, e1 = (1.0 / abs(e)) * e
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 260, in __rmul__
    return Vec2D(self[0]*other, self[1]*other)
  File "C:\Users\Nada Adel\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 251, in __new__
    return tuple.__new__(cls, (x, y))
RecursionError: maximum recursion depth exceeded while calling a Python object
Fatal Python error: _Py_CheckRecursiveCall: Cannot recover from stack overflow.
Python runtime state: initialized
 from turtle import Turtle, Screen
playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Turtle Keys")

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)

follow2 = Turtle("turtle")
follow2.speed("fastest")
follow2.color("purple")
follow2.penup()
follow2.shape()
follow2.setposition(250, -250)
follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.shape()
follow.setposition(-250, -250)

def k1():
    run.forward(10)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(10)

def quitThis():
    playGround.bye()

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(5)
    playGround.ontimer(follow_runner, 10)
    follow2.setheading(follow2.towards(run))
    follow2.forward(5)
    playGround.ontimer(follow_runner, 10)


playGround.onkeypress(k1, "Up")
playGround.onkeypress(k2, "Left")
playGround.onkeypress(k3, "Right")
playGround.onkeypress(k4, "Down")
playGround.onkey(quitThis, 'q')

playGround.listen()

follow_runner()

playGround.mainloop()

我尝试让一只乌龟流向另一只乌龟,效果很好。

from turtle import Turtle, Screen
playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Turtle Keys")

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)


follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.shape()
follow.setposition(-250, -250)

def k1():
    run.forward(10)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(10)

def quitThis():
    playGround.bye()

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(5)
    playGround.ontimer(follow_runner, 10)


playGround.onkeypress(k1, "Up")
playGround.onkeypress(k2, "Left")
playGround.onkeypress(k3, "Right")
playGround.onkeypress(k4, "Down")
playGround.onkey(quitThis, 'q')

playGround.listen()

follow_runner()

playGround.mainloop()
python tkinter turtle-graphics
1个回答
0
投票

使用您的代码,我想说问题是您对

playGround.ontimer(follow_runner, 10)
中的
follow_runner()
的两次调用。 函数末尾的一次调用就足够了,两次调用会导致调用树不断增长,最终看起来像 Python 的虚假递归:

from turtle import Turtle, Screen

def k1():
    run.forward(10)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(10)

def quitThis():
    playGround.bye()

def follow_runner():
    follow_1.setheading(follow_1.towards(run))
    follow_1.forward(5)

    follow_2.setheading(follow_2.towards(run))
    follow_2.forward(5)

    playGround.ontimer(follow_runner, 10)

playGround = Screen()
playGround.screensize(500, 500)
playGround.title("Turtle Keys")

run = Turtle('turtle')
run.speed('fastest')
run.penup()
run.color('blue')
run.setposition(250, 250)

follow_1 = run.clone()
follow_1.color('red')
follow_1.setposition(-250, -250)

follow_2 = run.clone()
follow_2.color('purple')
follow_2.setposition(250, -250)

playGround.onkeypress(k1, 'Up')
playGround.onkeypress(k2, 'Left')
playGround.onkeypress(k3, 'Right')
playGround.onkeypress(k4, 'Down')
playGround.onkey(quitThis, 'q')

playGround.listen()

follow_runner()

playGround.mainloop()

如果您希望两个跟随者相对于 ontimer()

不同
的速度移动,这是可能的,但不是您实现的方式。

© www.soinside.com 2019 - 2024. All rights reserved.