From: Harry Truax <htruax / stf.com>
Subject: Help needed with a TK Listbox
Date: Wed, 11 Jul 2012 02:24:13 +0900
Message-ID: <CAM8KA+Hc5pgV+Q31u_2FthAPCtkEc67GqR0ihr+9xObjD110uw / mail.gmail.com>

> I have a TK listbox, it is set for selectmode=>"single". All I want to do
> is de-select the selected list item when clicked on. So basically it would
> toggle between being selected [highlighted in blue on the display] and not
> selected [no blue highlighting]. I cannot use selectmode=>"multiple", as
> only one item can be selected at a time. I would rather not use a button
> and bind to that either.

I recommend you to use TkBindTag and Tk.callback_break.
For example,
-------------------------------------------------
require 'tk'

lbox = Tk::Listbox.new(:activestyle=>:none).pack(:fill=>:both, :expand=>true)
lbox.insert :end, *%w(a b c d e f g)


btag = TkBindTag.new
lbox.bindtags_unshift btag

btag.bind('1', :widget, :y){|w, y|
  idx = w.nearest y

  if idx == w.curselection[0]
    w.selection_clear idx
    Tk.callback_break
  else
    w.selection_set idx
    # Tk.callback_continue
  end
}

Tk.mainloop
-------------------------------------------------
-- 
Hidetoshi NAGAI (nagai / ai.kyutech.ac.jp)
Department of Artificial Intelligence, Kyushu Institute of Technology