Skip to content Skip to sidebar Skip to footer

Python Tkinter - Recovering Original Default Key Binding

I'm working with Python 2.7 and a Tkinter GUI on a Win7 machine. There are situations where I want to completely override the normal default behavior of the Tab key, but only so lo

Solution 1:

Okay, I tried it out and got an answer for you. What I suggested in my comment works. In summary, return "break" when you want to override it. Don't when you don't. Use bind instead of bind_all. And, if you're not already doing so in your args, factor in the event parameter, as it won't work properly otherwise. You're actually still binding something to the key, but it only overrides the default functionality in some the specified circumstance. Here's the code (adapted for both Python 2.x and 3.x):

import sys
if sys.version_info[0]<3:
    import Tkinter as tk
else:
    import tkinter as tk

#Root window
root = tk.Tk()

#Tab override handler
def overrideTab(*args):
    root.bind('<Tab>', stopTab)

def stopTab(event=None, *args):
    if ctrlChk4.get()==1:
        print('Tab is overridden')
        return "break"
    else:
        print('Tab is not overridden') #Note that it still prints this.

#Control variable
ctrlChk4 = tk.IntVar()
ctrlChk4.trace('w',overrideTab)

#GUI widgets
fra1  = tk.Frame(root)
chk1 = tk.Checkbutton(fra1,
                      text='First checkbutton')
chk2 = tk.Checkbutton(fra1,
                      text='Second checkbutton')
chk3 = tk.Checkbutton(fra1,
                      text='Third checkbutton')
chk4 = tk.Checkbutton(fra1,
                      text='Tab override',
                      variable=ctrlChk4)

fra1.grid(row=0,column=0,sticky=tk.W,padx=10,pady=10)
chk1.grid(row=0,column=0,sticky=tk.W,padx=(10,0),pady=(5,0))
chk2.grid(row=1,column=0,sticky=tk.W,padx=(10,0),pady=(5,0))
chk3.grid(row=2,column=0,sticky=tk.W,padx=(10,0),pady=(5,0))
chk4.grid(row=3,column=0,sticky=tk.W,padx=(10,0),pady=(5,0))

tk.mainloop()

Post a Comment for "Python Tkinter - Recovering Original Default Key Binding"