From: nagai@... Date: 2003-01-31T11:03:30+09:00 Subject: Re: TkListbox -- binding an entry Hi, From: Daniel Carrera Subject: TkListbox -- binding an entry Date: Fri, 31 Jan 2003 07:46:33 +0900 Message-ID: <20030130224627.GA2517@math.umd.edu> > I have a TkListbox, and I'd like to take an action when the user clicks on > a list entry. There are two ways that this might be possible: > * Bind each entry to a callback. > * Bind the list box to a callback and then find the entry that is > highlighted. I recommend you to use TkListbox#nearest method and binding to listbox widget. A sample script is here. ----------------------------------------------------- #!/usr/bin/env ruby require 'tk' f = TkFrame.new.pack lbox = TkListbox.new(f,'selectmode'=>'multiple').pack('side'=>'left') ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh', 'iii', 'jjj', 'kkk', 'lll', 'mmm', 'nnn'].each{|term| lbox.insert 'end',term } lbox.yscrollbar(TkScrollbar.new(f).pack('side'=>'left', 'fill'=>'y')) tag = TkBindTag.new lbox.bindtags(lbox.bindtags.unshift(tag)) tag.bind('Button-1', proc{|w,y| p w.get(w.nearest(y)) Tk.callback_break # block original binding of listbox widget }, '%W %y') Tk.mainloop ------------------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)