I'm trying to bind the SHIFT+TAB keys, but I can't seem to get it to work. The widget I'm binding to is an Entry
widget.
I've tried binding the keys with widget.bind('<Shift_Tab>', func)
, but I get an error message saying:
File "/usr/lib64/python3.8/tkinter/init.py", line 1337, in _bind self.tk.call(what + (sequence, cmd)) _tkinter.TclError: bad event type or keysym "Shift_Tab"
I'm still having a problem detecting SHIFT+TAB. Here is my test code. My OS is Linux. The tab key works, just not SHIFT+TAB. Seems like a simple problem to solve, so I must be going about it wrong?
I'm trying to tab between columns in a Treeview
that I have overlaid widgets on a row to simulate inline editing. There can be only one active widget on a line. I keep track of what column I'm in and when the user presses SHIFT+TAB or TAB, I remove the current widget and display a new widget in the corresponding column.
Here is a link to the complete project:
The project is in one file and has no imports.
The code below is my attempt and it doesn't work.
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.title('Default Demo')
self.geometry('420x200')
wdg = ttk.Entry(self)
wdg.grid()
def tab(_):
print('Tab pressed.')
def shift_tab(_):
print('Shift tab pressed.')
wdg.bind('<Tab>', tab)
wdg.bind('<Control-ISO_Left_Tab>', shift_tab)
def main():
app = App()
app.mainloop()
if __name__ == '__main__':
main()
In the below example we see how to bind the mouse click events on a tkinter window to a function call. In the below example we call the events to display the left-button double click, right button click and scroll-button click to display the position in the tkinter canvas where the buttons were clicked.
Use the bind() method to bind an event to a widget. Tkinter supports both instance-level and class-level bindings.
The right mouse button is pressed with the mouse pointer over the widget and this triggers an event which is handled by the event handler( do_popup() function ). The do_popup() function displays the menu.
Not a direct answer and too long for a comment.
You can solve your question by yourself with a simple trick, bind <Key>
to a function, and print the key event argument passed to the bind
function where you can see which key is pressed or not. Try multiple combinations of keys to see what is the state
and what is their keysym
or keycode
.
import tkinter as tk
def key_press(evt):
print(evt)
root = tk.Tk()
root.bind("<Key>", key_press)
root.mainloop()
It will output the following for pressing SHIFT + TAB combo on macOS.
<KeyPress event state=Shift keysym=Tab keycode=3145753 char='\x19' x=-5 y=-50>
Where,
state=Shift
means a key event's state is on SHIFT.
keysym=Tab
means tab key is pressed. If we just press SHIFT, the keysym
will be Shift_L or Shift_R (shows Shift_L for both shift keys on mac).
keycode
is an unique code for each key even for different key combos for example keycode for Left Shift is 131330
and keycode for TAB is 3145737
but when SHIFT + TAB is pressed the code is not the same to either, it is 3145753
. (I'm not sure if these are the same code for every os but one can figure it out by printing them to the console)
Also, see all the event attributes.
Though bind '<Shift-Tab>'
key combination works well on Mac, it can also be used like so...
def key_press(evt):
if evt.keycode==3145753:
print('Shift+Tab is pressed')
The following code (in python 2.7.6) should make it clear: I hope this reference works for you
from Tkinter import *
def key(event=None):
print 'You pressed Ctrl+Shift+Tab'
root = Tk()
frame = Frame(root, width=100, height=100)
frame.focus_set()
frame.bind('<Control-Shift-KeyPress-Tab>', key)
frame.pack()
root.mainloop()
EDIT: The above works well for Windows and Mac. For Linux, use
'<Control-ISO_Left_Tab>'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With