1

My question is related to this one where a Text widget is used.

However, in my case I want to rebind the select all on the entry widget.

I tried the following which allows me to use Ctrl+w to select all input in the entry field:

self.frmSearch = Frame()
self.txtSearch = Entry(self.frmSearch, bd=1, width=35)
self.txtSearch.bind('<Control-w>',lambda e: self.txtSearch.select_range(0, END))

However, once I change Ctrl+w to Ctrl+a this does not work anymore and no text is selected. Does anyone have an explanation why?

Community
  • 1
  • 1
orschiro
  • 19,847
  • 19
  • 64
  • 95

1 Answers1

4

It is because you are putting the binding on the widget rather than the widget class, and by default the bindings on the class fire after the bindings on the widget.

The way Tkinter processes events is to first see if there is a binding on a widget, then on a class, and then on the toplevel window, and then finally on the special class "all". The events are processed in order unless you break the chain of events, so to speak. So, your control-w binding happens, but then the binding on the class happens and effectively undoes what you you did in your binding.

The best solution is to 1) not use lambda, but instead use a real method or function, and 2) do a "return 'break'" which prevents the class and other bindings from firing. Or, if you want this binding to affect all entry widgets in your application rather than just a specific one, use bind_class giving the class name of 'Entry'.

The question you refer to you in your question has an answer that gives an example of changing the class binding.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685