Python Turtle计划在tkinter回调中给出“例外”。错误

发布于 2025-01-29 08:11:07 字数 3805 浏览 5 评论 0原文

我试图编写一条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()

I am trying to write a Python program of two turtles chasing another turtle. Yet the code crashes a few seconds into the game giving me an "Exception in Tkinter callback"

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

i tried it for one turtle fllowing another and it worked just fine.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2025-02-05 08:11:07

播放您的代码时,我会说问题是您打到Playground.ontimer(luct_runner,10) in collow> last_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(),这是可能的,但不是您实现的方式。

Playing with your code, I'd say the problem is your two calls to playGround.ontimer(follow_runner, 10) in follow_runner(). One call at the end of the function is sufficient, two cause a growing tree of calls that ends up looking like a faux recursion to 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()

If you want the two followers to move at different speeds with respect to ontimer(), that's possible but not the way you've implemented it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文