gpt4 book ai didi

python - 调用 python 对象时超出最大递归深度

转载 作者:行者123 更新时间:2023-11-28 20:22:36 24 4
gpt4 key购买 nike

我的目标是实例化一个名为箭头的类,这样我就可以拥有比 1 个更多的箭头。我想从坐标 200、200 开始,并希望每 100 毫秒将 x 增加 15。但是,当我尝试执行此代码时,出现以下错误:

  File "game.py", line 25, in moveArrow
self.after(100, self.moveArrow(arrow, xCoord+15, yCoord)) #repeat, changing x
File "game.py", line 24, in moveArrow
arrow.place(x = xCoord, y = yCoord) #replace with new x,y
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1860, in place_configure
+ self._options(cnf, kw))
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1055, in _options
elif isinstance(v, (tuple, list)):
RuntimeError: maximum recursion depth exceeded while calling a Python object

“文件“game.py”,第 25 行,在 move Arrow self.after(100, self.moveArrow(arrow, xCoord+15, yCoord)) #repeat, changing x 中也经常重复。

from Tkinter import *
from random import randint
from PIL import ImageTk, Image

class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master, height=400, width=400)
self.master = master
self.master.bind('<Shift_L>', self.createArrow)
def createArrow(self, event):
self.arrow = Arrow(self)
self.arrow.moveArrow(self.arrow, 200, 200)

class Arrow(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.arrowImage = ImageTk.PhotoImage(Image.open("arrow.gif"))
Label(self, image=self.arrowImage).pack()
def moveArrow(self, arrow, xCoord, yCoord):
arrow.place_forget()
arrow.place(x = xCoord, y = yCoord)
self.after(100, self.moveArrow(arrow, xCoord+15, yCoord))

root = Tk()
root.title("Mein erstes Spiel")
app = App(master=root).pack()
root.mainloop()

最佳答案

关于问题的根源是这一行的其他答案是正确的:

self.after(100, self.moveArrow(arrow, xCoord+15, yCoord))

但答案是特定于 Tkinter 的:

查看 after 的文档方法以查看如何正确实现此方法。像普通函数调用一样调用它会做到这一点,并在控制流到达该函数调用时将您的程序抛入无限循环。当您使用 after 时,您有两个选择:

传递时间参数,然后是回调,然后是回调参数:

self.after(100, self.moveArrow, arrow, xCoord+15, yCoord)

或者,使用 lambda 表达式来保存函数调用:

self.after(100, lambda: self.moveArrow(arrow, xCoord+15, yCoord))

关于python - 调用 python 对象时超出最大递归深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22955575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com