我的目标是实例化一个名为箭头的类,所以我可以有更多的箭头然后只有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
Run Code Online (Sandbox Code Playgroud)
"文件"game.py",第25行,在移动箭头self.after(100,self.moveArrow(箭头,xCoord + 15,yCoord))#repeat,更改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()
Run Code Online (Sandbox Code Playgroud)
其他答案是正确的问题来源是这一行:
self.after(100, self.moveArrow(arrow, xCoord+15, yCoord))
Run Code Online (Sandbox Code Playgroud)
但答案是Tkinter具体:
查看该after
方法的文档,了解如何正确实现此方法.将其称为普通函数调用就可以实现这一点,并在控制流到达该函数调用时将程序抛入无限循环.使用时after
,您有两种选择:
传递时间arg,然后是回调,然后是回调args:
self.after(100, self.moveArrow, arrow, xCoord+15, yCoord)
Run Code Online (Sandbox Code Playgroud)
或者,使用lambda表达式来保存函数调用:
self.after(100, lambda: self.moveArrow(arrow, xCoord+15, yCoord))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6763 次 |
最近记录: |