pythonuser-interfacetkinterbindunbind

How can I unbind every single binding from a Tkinter root window


So I have an app in Tkinter that has a lot of buttons in the first screen and when you press one you pass into a new "Window" (basically destroying all widgets and drawing the ones that are needed for the 'window'). There is a standard function that uses some commands to destroy every child on the root. I would like to add some code that can unbind all of the bindings that are made in the root. Bindings that are on specific widgets get destroyed but those that are bind on the root stay there and cause error.

Here's the code for destroying the widgets.

@staticmethod
def clear():
    for widget in guihandle.root.winfo_children():
        widget.destroy()

@staticmethod
def set(db,table):

    guihandle.clear()
    curW = Window(db,table)
    guihandle.current_Window = curW
    curW.initialize()
    guihandle.windows.push(curW)

(Yes, I make the base GUI from a sqlite3 database :P)


Solution

  • There is nothing in Tkinter to do what you want. Your app will need to keep track of the bindings it wants to remove.

    That being said, depending on just how complex your real problem is, there may be other solutions. For example, instead of binding to the root window, bind to a custom binding tag (also called a bind tag or bindtag). You will then need to add that tag to every widget to enable the bindings, and remove the tag from any existing widgets to effectively disable the bindings.