调用python对象时超出最大递归深度 [英] maximum recursion depth exceeded while calling a python object

查看:41
本文介绍了调用python对象时超出最大递归深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My aim is to instantiate a class called arrow, so I can have more arrows then just 1. I wanted to start with the coordinates 200, 200 and want to increase x by 15 per every 100 milliseconds. But when I try to execute this code it gives me following error:

  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

"File "game.py", line 25, in move Arrow self.after(100, self.moveArrow(arrow, xCoord+15, yCoord)) #repeat, changes x" 也经常重复.

The "File "game.py", line 25, in move Arrow self.after(100, self.moveArrow(arrow, xCoord+15, yCoord)) #repeat, changing x" gets repeated very often as well.

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

推荐答案

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

The other answers are correct about the source of the problem being this line:

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

但答案是特定于 Tkinter 的:

But the answer is Tkinter specific:

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

Take a look at the docs for the after method to see how to properly implement this method. Calling it like a normal function call will do just that and throw your program into an infinite loop when the control flow reaches that function call. When you use after, you have two options:

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

Passing the time arg, then the callback, then the callback args:

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

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

Or, using a lambda expression to hold the function call:

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

这篇关于调用python对象时超出最大递归深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆