Jozef Vitko
Jozef Vitko

Reputation: 11

python bind not working

I have the basic screensaver program. I want to bind Mouse button 1 to delete everything on screen, but when I press the Button-1 I get this error:

 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

. . . . This is my program:

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)

Even when I change what is in the def delete I still get the error. Thanks for your time! I am new to this so I have no idea where is the problem ....................................................................................................................................................................................................................................................

Upvotes: 1

Views: 778

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

Functions called via bindings are automatically passed an object that represents the event. Your delete method needs to be able to accept this parameter even if you don't use it.

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

Upvotes: 2

Related Questions