summaryrefslogtreecommitdiffstats
path: root/Lib/tkinter
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-12-06 15:12:37 (GMT)
committerGitHub <noreply@github.com>2023-12-06 15:12:37 (GMT)
commitf189bd3e839f95396ca4db3295883976c510ef0d (patch)
tree71ec21fdf3728bc57e5b40bfbe34fb0093cdbb6f /Lib/tkinter
parent399a3f2e1e428710fa4dc5c54841e179dc8e1028 (diff)
downloadcpython-f189bd3e839f95396ca4db3295883976c510ef0d.zip
cpython-f189bd3e839f95396ca4db3295883976c510ef0d.tar.gz
cpython-f189bd3e839f95396ca4db3295883976c510ef0d.tar.bz2
[3.12] gh-75666: Tkinter: "unbind(sequence, funcid)" now only unbinds "funcid" (GH-111322) (GH-112802)
Previously, "widget.unbind(sequence, funcid)" destroyed the current binding for "sequence", leaving "sequence" unbound, and deleted the "funcid" command. Now it removes only "funcid" from the binding for "sequence", keeping other commands, and deletes the "funcid" command. It leaves "sequence" unbound only if "funcid" was the last bound command. (cherry picked from commit cc7e45cc572dd818412a649970fdee579417701f) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: GiovanniL <13402461+GiovaLomba@users.noreply.github.com>
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):