Rubytk - How to use <<ListboxSelect>>

E

Enrico Schwass

Hello

In the tk reference manual i found an event called ListboxSelect.
I would like to use this to change a buttons state from :normal to
:disabled or vice versa.
Say a listbox is empty. Now, edit an entry is not possible so the button
should disabled.
After a few seconds the listbox get filled, the edit button should now set
to normal state.

Could you post a code snippet to demonstrate this callback functionality

Thanks
Enno
 
H

Hidetoshi NAGAI

From: "Enrico Schwass" <[email protected]>
Subject: Rubytk - How to use <<ListboxSelect>>
Date: Mon, 13 Jun 2005 04:06:24 +0900
Message-ID: said:
In the tk reference manual i found an event called ListboxSelect. (snip)
Could you post a code snippet to demonstrate this callback functionality

How about the following?
--------------------------------------------------
require 'tk'

f = TkFrame.new.pack:)fill=>:x)

entry = TkEntry.new(f).pack:)side=>:left, :expand=>true, :fill=>:x)

b_add = TkButton.new(f, :text=>'ADD', :state=>:normal).pack:)side=>:right)
b_edit = TkButton.new(f, :text=>'EDIT', :state=>:disabled).pack:)side=>:right)

available = b_add
entry.bind('Return', proc{available.invoke})

lbox = TkListbox.new:)selectmode=>:multiple).pack:)fill=>:both, :expand=>true)

old_sel = []
lbox.bind('<ListboxSelect>'){
sel = (lbox.curselection - old_sel)
if sel.empty?
entry.value = ''
b_add[:state] = :normal
b_edit[:state] = :disable
available = b_add
else
lbox.selection_clear(old_sel[0]) unless old_sel.empty?
entry.value = lbox.get(sel[0])
b_add[:state] = :disable
b_edit[:state] = :normal
available = b_edit
end
old_sel = sel
}

b_add.command{
unless (val = entry.value).empty?
lbox.insert:)end, val)
entry.value = ''
end
}

b_edit.command{
unless (val = entry.value).empty?
idx = lbox.curselection[0]
lbox.delete(idx)
lbox.insert(idx, val)
lbox.selection_set(idx)
end
}

entry.focus

Tk.mainloop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top