dimanche 19 juin 2016

Unable to unbind a function using tkinter


I am working with Tkinter in Python 3.5 and I encounter a weird problem.

I used the tkinterbook about events and bindings to write this simple example:

from tkinter import *

root = Tk()

frame = Frame(root, width=100, height=100)

def callback(event):
    print("clicked at", event.x, event.y)
    # frame.unbind("<Button-1>", callback)

frame.bind("<Button-1>", callback)
frame.pack()

root.mainloop()

It works fine, but if I try to unbond the callback (just uncomment the line), it fails with the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:UsersDelganAppDataLocalProgramsPythonPython35libtkinter__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:UsersDelganDesktopTesttest.py", line 9, in callback
    frame.unbind("<Button-1>", callback)
  File "C:UsersDelganAppDataLocalProgramsPythonPython35libtkinter__init__.py", line 1105, in unbind
    self.deletecommand(funcid)
  File "C:UsersDelganAppDataLocalProgramsPythonPython35libtkinter__init__.py", line 441, in deletecommand
    self.tk.deletecommand(name)
TypeError: deletecommand() argument must be str, not function

This is not clear, I am not sure if it is a bug in tkinter or if I am doing something wrong.

frame.unbind("<Button-1>") works fine but I would like to remove this exact callback instead of a global remove.


Aucun commentaire:

Enregistrer un commentaire