Forum: Ruby Need help to select some listbox item in different listbox together

Posted by Jonathan Masato (redturbo5)
on 2012-12-07 09:13
Hello,

Does anyone know how to do the following:

I have a problem when I want to Select some listbox item in different
listbox in the same time together. I have found a code and make a little
changing to the code. In that code there are 3 listbox with different
item, when I select the first item (first listbox), the selection is
only show in the third item (in the third listbox), whereas I want to
make all of the third item in the same row in that three different
listbox selected together.


Here is mycode :
require 'tkextlib/tile'

$countrycodes = %w{ ar au be br ca cn dk fi fr gr in it jp mx nl no es
se ch }
$countrynames = %w{ Argentina Australia Belgium Brazil Canada China
Denmark
        Finland France Greece India Italy Japan Mexico Netherlands
Norway Spain
        Sweden Switzerland}
$nomor = %w{ 1 2 3 4 5 6 7
        8 9 10 11 12 13 14 15 16 17
        18 19}

$names = TkVariable.new ( $countrynames )
$kode = TkVariable.new ( $countrycodes )
$no = TkVariable.new ( $nomor )


root = TkRoot.new
content = Tk::Tile::Frame.new(root) {padding "5 5 12 0"}.grid :column =>
0, :row => 0, :sticky => "nwes"
TkGrid.columnconfigure root, 0, :weight => 1
TkGrid.rowconfigure root, 0, :weight => 1


$countries = TkListbox.new(content) {listvariable $names; height
5;selectmode 'multiple'}
$kode = TkListbox.new(content) {listvariable $kode; height 5;selectmode
'multiple'}
$no = TkListbox.new(content) {listvariable $no; height 5l;selectmode
'multiple'}

$countries.grid :column => 0, :row => 0, :rowspan => 6, :sticky =>
'nsew'
$kode.grid :column => 1, :row => 0, :rowspan => 6, :sticky => 'nsew'
$no.grid :column => 2, :row => 0, :rowspan => 6, :sticky => 'nsew'
TkGrid.columnconfigure content, 0, :weight => 1
TkGrid.rowconfigure content, 5, :weight => 1

$countries.bind '<ListboxSelect>', proc{pilihAnggota}

def pilihAnggota
    idx = $countries.curselection
      $kode.selection_set idx
  $no.selection_set idx
end





# Colorize alternating lines of the listbox
0.step($countrycodes.length-1, 2) {|i| $countries.itemconfigure i,
:background, "#f0f0ff"}
0.step($countrycodes.length-1, 2) {|i| $kode.itemconfigure i,
:background, "#f0f0ff"}
0.step($countrycodes.length-1, 2) {|i| $no.itemconfigure i, :background,
"#f0f0ff"}

$countries.selection_set 0
#showPopulation

Tk.mainloop

================================================================
Thank's for your help.
Posted by Joel Pearson (virtuoso)
on 2012-12-07 10:15
Is there a particular reason you want to "select" them? Why don't you 
just colour them?
Posted by Jonathan Masato (redturbo5)
on 2012-12-07 14:38
Joel Pearson wrote in post #1088182:
> Is there a particular reason you want to "select" them? Why don't you
> just colour them?

My reason is : I want to make that like a table so if I select a row,
all column in that row is selected. Can you help me ?
Posted by Joel Pearson (virtuoso)
on 2012-12-07 15:24
I did experiment briefly with multidimensional listboxes in Tk, but I 
found it much simpler to use colouring to emulate row selection, since I 
only needed the first cell as a reference. I couldn't find any 
documentation on it at the time so I was creating multiple listboxes and 
firing events to link them.
All you'd need to do then is alter the colour of each relevant cell 
based on the selection, and it'll look like it's selected; while you 
have the other column's selection value to work from.

The other problem I encountered was using a scrollbar on multiple 
listboxes. If you've found a way around that kind of thing I'd be 
interested to know how you did it.
Posted by Jonathan Masato (redturbo5)
on 2012-12-07 17:00
Joel Pearson wrote in post #1088221:
> I did experiment briefly with multidimensional listboxes in Tk, but I
> found it much simpler to use colouring to emulate row selection, since I
> only needed the first cell as a reference. I couldn't find any
> documentation on it at the time so I was creating multiple listboxes and
> firing events to link them.
> All you'd need to do then is alter the colour of each relevant cell
> based on the selection, and it'll look like it's selected; while you
> have the other column's selection value to work from.
>
> The other problem I encountered was using a scrollbar on multiple
> listboxes. If you've found a way around that kind of thing I'd be
> interested to know how you did it.

Thanks for your suggestion.
May I ask the sample code to use colouring to emulate row selection ?
I still new in Ruby.
Posted by Jonathan Masato (redturbo5)
on 2012-12-07 17:58
Joel Pearson wrote in post #1088221:
> I did experiment briefly with multidimensional listboxes in Tk, but I
> found it much simpler to use colouring to emulate row selection, since I
> only needed the first cell as a reference. I couldn't find any
> documentation on it at the time so I was creating multiple listboxes and
> firing events to link them.
> All you'd need to do then is alter the colour of each relevant cell
> based on the selection, and it'll look like it's selected; while you
> have the other column's selection value to work from.
>
> The other problem I encountered was using a scrollbar on multiple
> listboxes. If you've found a way around that kind of thing I'd be
> interested to know how you did it.

For your problem to using scrollbar, I get an idea to put all of the 
listboxes in one frame, and we limit the height of the frame, then the 
scrollbar placed in the frame. But I still not implemented that, that 
still just an idea.
Posted by Joel Pearson (virtuoso)
on 2012-12-07 20:57
> May I ask the sample code to use colouring to emulate row selection ?
> I still new in Ruby.

You'll need to find a colour which suits your interface (I've just kept
green from my line of code); and I didn't actually use the selection
event of the listbox so you'd need to trap that first, but this code
will colour an item:

@ListBox1.itemconfigure(1, :background => "green")

"1" will be the index of whichever item you had selected. What you'd
then need to do is send the same index value to your other columns:

@ListBox2.itemconfigure(list_1_items.index(1), :background => "green")
@ListBox3.itemconfigure(list_1_items.index(1), :background => "green")

Or like this:

idx = 1

[@ListBox1, @ListBox2, @ListBox3].each do |current_object|
current_object.itemconfigure(idx), :background => "green")
end

Another trick is if you know the value of the cell selected in the first
column (first listbox), and if you have the array which corresponds to
that list. What you can do is retrieve the index from the array of
values.
So say your array is ["red","blue","yellow"], and you wanted to find
"blue", the code would be:

["red","blue","yellow"].index("blue")
=>1
Posted by Joel Pearson (virtuoso)
on 2012-12-07 21:07
Ok I've had time to try running your code. The problem you seem to have 
is that if you have more than 1 item selected, you get the indices back 
as an array. When you try to pass that array on it'll give you an error.
You either need to break down the indices in your code and loop through 
them, or make your listbox single-selection.
Posted by Joel Pearson (virtuoso)
on 2012-12-07 21:17
> May I ask the sample code to use colouring to emulate row selection ?
> I still new in Ruby.


Here's your method rewritten to colour all selected rows in blue:



def pilihAnggota

  #Get our array of indicies
    idx = $countries.curselection

  #Default everything to white
  [$countries,$kode,$no].each do |obj|
    obj.size.times do |ix|
      obj.itemconfigure(ix, :background => "white")
    end
  end

  #Colour all selected indicies to blue
  idx.each do |ix|
    [$countries,$kode,$no].each do |obj|
      obj.itemconfigure(ix, :background => "blue")
    end
  end
end
Posted by Mas Bejo (masbejo)
on 2012-12-08 04:09
i have same problem, i have been try like with code.

def pilihAnggota

  #Get our array of indicies
    idx = $countries.curselection

  #Default everything to white
  [$countries,$kode,$no].each do |obj|
    obj.size.times do |ix|
      obj.itemconfigure(ix, :background => "white")
    end
  end

  #Colour all selected indicies to blue
  idx.each do |ix|
    [$countries,$kode,$no].each do |obj|
      obj.selection_set idx
    end
  end

end

but the problem, it just select in last list. can you help me?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.