【问题标题】:python tkinter moving circle with cursorpython tkinter用光标移动圆圈
【发布时间】: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

标签: python tkinter


【解决方案1】:

您应该从字面上理解此错误消息。 “cursor() 正好有 1 个参数(给定 0)”消息的意思 游标函数被定义为接受事件作为参数def cursor(event): 但随后您无需任何参数即可调用它g1, g2 = cursor()

【讨论】:

    猜你喜欢
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多