Help needed with a TK Listbox

Hello,

Does anyone know how to do the following:

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.

Thanks for your help,

Harry T.

From: Harry T. [email protected]
Subject: Help needed with a TK Listbox
Date: Wed, 11 Jul 2012 02:24:13 +0900
Message-ID:
[email protected]

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

Thanks!