Reputation: 21
I have a text field in a GUI and I'm trying to bind the tab key to an event in my class. This usually works just fine but when I try to bind it with bindtags, the tab key doesn't trigger any events. Other keypresses like return and a still work.
Code:
import tkinter as tk
import tkinter.scrolledtext as tkscrolled
class GUI():
def __init__(self):
self.root = tk.Tk()
self.t = tkscrolled.ScrolledText(self.root, height=20, width=30)
self.t.pack()
self.t.bindtags(('.!frame.!frame.!scrolledtext', "Text", "enter", "tab", ".", "all"))
self.t.bind_class("enter", '<Return>', self.enter_return)
self.t.bind_class("tab", '<Tab>', self.enter_tab)
self.root.mainloop()
def enter_return(self, event):
print("Return")
def enter_tab(self, event):
print("Tab")
GUI()
When I press tab it is expected to print("Tab") but instead nothing happens. The enter still works.
EDIT: I am now even more confused because it triggers when doing ctrl+tab but not tab alone...
Upvotes: 2
Views: 119
Reputation: 386332
The default binding for the tab key in a text widget (the "Text" binding tag) will call break
, preventing any other processing of the event. Since the binding tag "tab" comes after the binding for the class "Text", the binding to "tab" will not be processed.
If you move "tab" before "Text" in the bindtags, your binding will trigger before the class binding.
Upvotes: 1