Skip to content

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,26 +479,46 @@ def test2(e): pass

def test_unbind2(self):
f = self.frame
f.wait_visibility()
f.focus_force()
f.update_idletasks()
event = '<Control-Alt-Key-c>'
self.assertEqual(f.bind(), ())
self.assertEqual(f.bind(event), '')
def test1(e): pass
def test2(e): pass
def test1(e): events.append('a')
def test2(e): events.append('b')
def test3(e): events.append('c')

funcid = f.bind(event, test1)
funcid2 = f.bind(event, test2, add=True)
funcid3 = f.bind(event, test3, add=True)
events = []
f.event_generate(event)
self.assertEqual(events, ['a', 'b', 'c'])

f.unbind(event, funcid)
f.unbind(event, funcid2)
script = f.bind(event)
self.assertNotIn(funcid, script)
self.assertCommandNotExist(funcid)
self.assertCommandExist(funcid2)
self.assertNotIn(funcid2, script)
self.assertIn(funcid, script)
self.assertIn(funcid3, script)
self.assertEqual(f.bind(), (event,))
self.assertCommandNotExist(funcid2)
self.assertCommandExist(funcid)
self.assertCommandExist(funcid3)
events = []
f.event_generate(event)
self.assertEqual(events, ['a', 'c'])

f.unbind(event, funcid2)
f.unbind(event, funcid)
f.unbind(event, funcid3)
self.assertEqual(f.bind(event), '')
self.assertEqual(f.bind(), ())
self.assertCommandNotExist(funcid)
self.assertCommandNotExist(funcid2)
self.assertCommandNotExist(funcid3)
events = []
f.event_generate(event)
self.assertEqual(events, [])

# non-idempotent
self.assertRaises(tkinter.TclError, f.unbind, event, funcid2)
Expand Down
22 changes: 18 additions & 4 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Member

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.

Copy link
Member Author

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 {"[140011831004144test1 %# %b %f %h %k %s %t %w %x %y %A %E %K %N %W %T %X %Y %D]" == "break"} break

if {"[140011830917168test2 %# %b %f %h %k %s %t %w %x %y %A %E %K %N %W %T %X %Y %D]" == "break"} break

if it only contains bindings added by bind(). It can also contain arbitrary Tcl commands, including multiline commands, if it is standard binding or bind() 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 generated funcid.

Copy link
Member Author

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().

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?

Copy link
Member

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.

Copy link
Member Author

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() with funcid will not touch it, and unbind() without second argument will remove it. If you use funcid in other binding, it will stop working when unbind() deletes the command. If you change the line that calls funcid, 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.

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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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):
Expand Down
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.