@@ -1503,13 +1503,26 @@ def bindtags(self, tagList=None):
15031503 else :
15041504 self .tk .call ('bindtags' , self ._w , tagList )
15051505
1506- def _bind (self , what , sequence , func , add , needcleanup = 1 ):
1506+ def _delete_bind_commands (self , * what ):
1507+ lines = self .tk .call (what ).split ('\n ' )
1508+ p = re .compile (r'if \{"\[([^ ]+) .*\]" == "break"\} break' )
1509+ for line in lines :
1510+ m = p .fullmatch (line )
1511+ if m :
1512+ funcid = m [1 ]
1513+ try :
1514+ self .deletecommand (funcid )
1515+ except TclError :
1516+ pass
1517+
1518+ def _bind (self , what , sequence , func , add ):
15071519 """Internal function."""
15081520 if isinstance (func , str ):
15091521 self .tk .call (what + (sequence , func ))
15101522 elif func :
1511- funcid = self ._register (func , self ._substitute ,
1512- needcleanup )
1523+ if not add :
1524+ self ._delete_bind_commands (* what , sequence )
1525+ funcid = self ._register (func , self ._substitute , needcleanup = True )
15131526 cmd = ('%sif {"[%s %s]" == "break"} break\n '
15141527 %
15151528 (add and '+' or '' ,
@@ -1575,6 +1588,7 @@ def unbind(self, sequence, funcid=None):
15751588
15761589 def _unbind (self , what , funcid = None ):
15771590 if funcid is None :
1591+ self ._delete_bind_commands (* what )
15781592 self .tk .call (* what , '' )
15791593 else :
15801594 lines = self .tk .call (what ).split ('\n ' )
@@ -1591,7 +1605,7 @@ def bind_all(self, sequence=None, func=None, add=None):
15911605 An additional boolean parameter ADD specifies whether FUNC will
15921606 be called additionally to the other bound function or whether
15931607 it will replace the previous function. See bind for the return value."""
1594- return self ._root ()._bind (('bind' , 'all' ), sequence , func , add , True )
1608+ return self ._root ()._bind (('bind' , 'all' ), sequence , func , add )
15951609
15961610 def unbind_all (self , sequence ):
15971611 """Unbind for all widgets for event SEQUENCE all functions."""
@@ -1605,7 +1619,7 @@ def bind_class(self, className, sequence=None, func=None, add=None):
16051619 whether it will replace the previous function. See bind for
16061620 the return value."""
16071621
1608- return self ._root ()._bind (('bind' , className ), sequence , func , add , True )
1622+ return self ._root ()._bind (('bind' , className ), sequence , func , add )
16091623
16101624 def unbind_class (self , className , sequence ):
16111625 """Unbind for all widgets with bindtag CLASSNAME for event SEQUENCE
0 commit comments