[Tkinter-discuss] How to prevent a selection when double-clicking?

Michael Lange klappnase at web.de
Sun May 20 12:31:42 CEST 2012


Thus spoketh Bryan Oakley <bryan.oakley at gmail.com> 
unto us on Sat, 19 May 2012 07:42:52 -0500:

> On Sat, May 19, 2012 at 12:42 AM, Juha Salo <juha.k.salo at gmail.com>
> wrote:
> > Hi!
> >
> > Is there a way to get rid of the selection (blue area in the window)
> > after double-clicking on a Text widget? I use Vista and Python 3.2.3.
> > I have the following example:
> >
> >
> The selection happens on a single click; are you wanting to keep it on a
> single-click, but remove it on a double click?

I do not have Vista at hand here, so I cannot tell if it is a single or a
double click that causes the selection [1], here on linux it is a
double-click that causes the rest of the line, after the "fooo" to be
selected if you click "somewhere" in the text or the "fooo" if you click
into it. You can get rid of this by either adding a

   return "break"

statement to the dbclick() callback or by adding

   root.unbind_class('Text', '<Double-Button-1>')

to your code *before* the text widget is created (of course this will do
the same for all Text widgets in you app though), doing a simple


does not seem to work, apparently because is it called *after* widget

If you want to check the default widget bindings, have a look at text.tcl
where these are defined, here the relevant code for single- and double
click looks like:

bind Text <1> {
    tk::TextButton1 %W %x %y
    %W tag remove sel 0.0 end
bind Text <Double-1> {
    set tk::Priv(selectMode) word
    tk::TextSelectTo %W %x %y
    catch {%W mark set insert sel.first}

Looking there is often a better way dealing with default bindings than
trying to understand them by clicking into the widget on a
trial-and-error base, because considerable confusion may arise by
interfering other bindings, for example there is also a <Triple-1>
binding on text widgets which might fire if you repeatedly double-click
into the widget.

I hope this helps


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Not one hundred percent efficient, of course ... but nothing ever is.
		-- Kirk, "Metamorphosis", stardate 3219.8

More information about the Tkinter-discuss mailing list