【发布时间】:2018-09-04 17:41:09
【问题描述】:
我正在尝试在 tkinter 中创建 agar.io 游戏。我试图用光标移动一个圆圈,但出现此错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1541, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 592, in callit
func(*args)
File "/Users/Hari/Desktop/Agario.py", line 32, in move1
g1, g2 = cursor()
TypeError: cursor() takes exactly 1 argument (0 given)
这是我的代码。这还不完整,我只想完成移动光标的键绑定,然后我将继续移动圆圈本身
import Tkinter
from random import randint
tk=Tkinter.Tk()
canvas=Tkinter.Canvas(width=1250, height=700)
canvas.configure(background='red')
frame=canvas.create_rectangle(10,10,1240,690, fill="white")
om=canvas.create_oval(50,50,75,75, fill="blue")
lis=[]
count=0
on=0
def move1():
global lis, count, on
count=count+1
if(count%100==0):
c1=randint(10,1235)
c2=randint(10,685)
o=canvas.create_oval(c1,c2,c1+5,c2+5, fill="green")
lis1=[]
lis1.append(c1)
lis1.append(c2)
lis1.append(c1+5)
lis1.append(c2+5)
lis.append(lis1)
g1, g2 = cursor()
print g1, g2
x1, y1, x2, y2=canvas.coords(om)
canvas.after(1,move1)
move1()
def cursor(event):
m1=event.x
m2=event.y
return m1, m2
tk.bind("<B1-Motion>", cursor)
canvas.pack()
tk.mainloop()
【问题讨论】:
-
错误信息看起来很不言自明。您的游标方法需要一个参数,但您在没有参数的情况下调用它。
-
您正在从
move中调用回调函数cursor,但您可能希望从cursor中调用move。