Deleting and changing a tkinter event binding

后端 未结 3 689
滥情空心
滥情空心 2020-12-05 18:23

How do i stop an event from being processed or switch what function is called for it?

Revised Code:

from Tkinter import *

class GUI         


        
3条回答
  •  攒了一身酷
    2020-12-05 19:10

    For me, unbinding a single callback wasn't working, but I found a solution.

    I can see this is an old question, but for those who, like myself, find this question when facing the same problem, this is what I did to make it work.

    You will need to open the source file Tkinter.py and search for the unbind method of the Misc class (if you are using eclipse it's easy to know the file's location and the line in which this function is defined by pressing the F3 key when the cursor is over an .unbind function call in your code).

    When you find it, you should see something like this:

    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:
                self.deletecommand(funcid)
    

    You need to change it to look somethins like this:

    def unbind(self, sequence, funcid=None):
            """Unbind for this widget for event SEQUENCE  the
            function identified with FUNCID."""
        if not funcid:
            self.tk.call('bind', self._w, sequence, '')
            return
        func_callbacks = self.tk.call('bind', self._w, sequence, None).split('\n')
        new_callbacks = [l for l in func_callbacks if l[6:6 + len(funcid)] != funcid]
        self.tk.call('bind', self._w, sequence, '\n'.join(new_callbacks))
        self.deletecommand(funcid)
    

    That should do the trick!

提交回复
热议问题