summaryrefslogtreecommitdiffstats
path: root/Lib/tkinter
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/tkinter')
-rw-r--r--Lib/tkinter/__init__.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index cb3e5f1..c47c8c2 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -1448,10 +1448,24 @@ class Misc:
return self._bind(('bind', self._w), sequence, func, add)
def unbind(self, sequence, funcid=None):
- """Unbind for this widget for event SEQUENCE the
- function identified with FUNCID."""
- self.tk.call('bind', self._w, sequence, '')
- if funcid:
+ """Unbind for this widget the event SEQUENCE.
+
+ If FUNCID is given, only unbind the function identified with FUNCID
+ and also delete the corresponding Tcl command.
+
+ Otherwise destroy the current binding for SEQUENCE, leaving SEQUENCE
+ unbound.
+ """
+ if funcid is None:
+ self.tk.call('bind', self._w, sequence, '')
+ else:
+ lines = self.tk.call('bind', self._w, sequence).split('\n')
+ prefix = f'if {{"[{funcid} '
+ keep = '\n'.join(line for line in lines
+ if not line.startswith(prefix))
+ if not keep.strip():
+ keep = ''
+ self.tk.call('bind', self._w, sequence, keep)
self.deletecommand(funcid)
def bind_all(self, sequence=None, func=None, add=None):