python绑定不起作用

时间:2018-05-27 10:52:51

标签: python tkinter bind tkinter-canvas

我有基本的屏幕保护程序。我想绑定鼠标按钮1以删除屏幕上的所有内容,但是当我按下Button-1时出现此错误:

 Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\idlelib\run.py", line 121, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Python34\lib\queue.py", line 175, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
TypeError: delete() takes 0 positional arguments but 1 was given

。 。 。 。 这是我的计划:

import tkinter
canvas=tkinter.Canvas(width=1000,height=800)
canvas.pack()
from random import *

entry1=tkinter.Entry()
entry1.pack()





def setric():





    x=randrange(900)
    y=randrange(700)




    global a
    canvas.delete('all')
    farba=choice(("red","purple","blue","lightblue","green","lightgreen"))

    if a>-90:
        a=a-10
    else:
        a=-10


    if entry1.get()=="":
        canvas.update()
        canvas.after(3000,setric)
        a=0


    else:

        canvas.create_text(x,y,text=entry1.get(),font="Arial 40",fill=farba,angle=a)

        canvas.after(2000, setric)
def delete():
    canvas.delete("all")
a=0        
setric()

canvas.bind('<Button-1>',delete)

即使我更改了def删除中的内容,我仍然会收到错误消息。 谢谢你的时间!我是新手,所以我不知道问题出在哪里 .................................................. .................................................. .................................................. .................................................. ............................................

1 个答案:

答案 0 :(得分:1)

通过绑定调用的函数会自动传递一个表示该事件的对象。即使您不使用此参数,您的delete方法也需要能够接受此参数。

def delete(event):
    canvas.delete("all")