I’m working with the Text widget and I have an issue about old-school shortcuts that Tk uses.
Ie:
Select all: Ctrl + / vs Ctrl + a
Cut: Ctrl + w vs Ctrl + x
Copy: Meta + w vs Ctrl + c
Paste: Ctrl + y vs Ctrl + v
On Windows, all of them work except Ctrl+a.
1) Is it possible to redirect binds, so .bind('<Control-a>') calls already bound Ctrl + /?
2) I tried for “select all”:
txt_text.bind('<Control-a>', self.ctext_selectall)
Where:
def ctext_selectall(self, callback):
"""Select all text in the text widget"""
self.txt_text.tag_add('sel', '1.0', 'end')
But it does not work, since Ctrl+a works by default (cursor goes to the start). It function with some other, unbound, letter. Any chances of making this work if the solution under 1 is not possible?
The default bindings are applied to the widget class. When you do a bind, it affects a specific widget and that binding happens before the class binding. So what is happening is that your binding is happening and then the class binding is happening, which makes it seem as if your binding isn’t working.
There are two ways to solve this. One, your
ctext_selectallcan return the string “break” which will prevent the class binding from firing. That should be good enough to solve your immediate problem.The second solution involves changing the class binding so that your preferred binding applies to all text widgets. You would do this using the
bind_classmethod.Here’s an example of rebinding the class:
effbot.org has a pretty decent writeup titled Events and Bindings. It goes into a little more detail about class and widget bindings and the order in which they occur.
Tk’s binding mechanism is about the best of any GUI toolkit there is. Once you understand how it works (and it’s remarkably simple) you’ll find it’s easy to augment or replace any or all of the default bindings.