-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-75666: Tkinter: "unbind(sequence, funcid)" now only unbinds "funcid" #111322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f8ad1a9
ba53020
776293a
3e71c69
f8b58bc
be3ba6a
c5b05e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1527,10 +1527,24 @@ def bind(self, sequence=None, func=None, add=None): | |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea if it is legal to bind a multiple-script script, hence test should require that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is supported, but neither documented nor tested. I have not touched this, because it can mean doubling or tripling the number of tests. |
||
self.deletecommand(funcid) | ||
|
||
def bind_all(self, sequence=None, func=None, add=None): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Fix the behavior of :mod:`tkinter` widget's ``unbind()`` method with two | ||
arguments. 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without seeing what
lines
looks like for various scenarios, I cannot verify the code below. Hence would have to trust test. Hence would like testing of what seems like a harder scenario.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually it looks like:
if it only contains bindings added by
bind()
. It can also contain arbitrary Tcl commands, including multiline commands, if it is standard binding orbind()
was called with a string instead of callable (AFAIK IDLE does such hacking). It does not matter, because it is unlikely that somebody add commands in the above form, using generatedfuncid
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked -- IDLE uses this in
fix_x11_paste()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's useful to manually register a string (to reuse functions multiple times or to directly call a Tk method), but that would entirely reasonably preclude using
unbind()
with that function. Should there be a test or something to verify it leaves other lines intact?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, you wrote that. This comment only serves to explain what I did not review in detail and why I requested the test change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TeamSpen210 If you manually register a string to directly call a Tk method,
unbind()
withfuncid
will not touch it, andunbind()
without second argument will remove it. If you usefuncid
in other binding, it will stop working whenunbind()
deletes the command. If you change the line that callsfuncid
,unbind()
may not find it or break the script after deleting the line. In any case, this feature is not documented, and doing so, you are at a risk. Of course, we will try to not break it, but our expectation of possible use cases can be wrong if you do something unusual.