I'm trying to use a list_box to select from different types of conversions. When I start the program and enter a number it does nothing, but if I click the next item in the list_box it works fine. This makes me think my method is not getting it's value from the list_box. Here's the code:
Shoes.app :title=> 'Temperature Converter', :width => 200, :height => 200 do
def convert(temp, unit='C')
if unit == "C"
(temp.to_i * 9.0 / 5.0) + 32.0
elsif unit == "F"
"Fail"
end
end
list_box :items => ["C", "F"], :choose => "C" do |item|
@unit.text = item.text
end
line1 = edit_line :width => 100
button 'Compute' do
@result.text = convert(line1.text, @unit.text)
end
@unit = para
@result = para
end
I tried setting 'C' as the default variable but that didn't work either. Is there a way to force the list_box to send it's value on startup?
Also, and unrelated, if I remove '@unit = para' from the end it won't print anything, even the @result. Why is that?
Any help would be awesome.
It probably won't print anything because your button and list_box are trying to call @unit.text
, so you must continue to define @unit
.
I think that is possibly the same reason that it won't choose "C" by default. At the time you choose "C", @unit
is not defined.
Try this:
@list_box = list_box :items => ["C", "F"]
line1 = edit_line :width => 100
button 'Compute' do
@result.text = convert(line1.text, @unit.text)
end
@unit = para
@list_box.change{|item|
@unit.text = item.text
}
@list_box.choose("C")
I am unsure whether you need to separate the change
method from the list box, but I have done so to be on the safe side.
EDIT: 2012-01-31 13:29
Shoes.app :title=> 'Temperature Converter', :width => 200, :height => 200 do
def convert(temp, unit='C')
@unit.text = unit
if unit == "C"
(temp.to_i * 9.0 / 5.0) + 32.0
elsif unit == "F"
"Fail"
end
end
@list_box = list_box :items => ["C", "F"]
line1 = edit_line :width => 100
button 'Compute' do
@result.text = convert(line1.text, @list_box.text)
end
@unit = para
@list_box.choose("C")
@result = para
end
The reason the op's code does not succeed in retrieving the initial value of the list box is because the block given to list_box() is only executed onchange, i.e. when the user changes the selection in the listbox. The button click in the op's code retrieves the current value of a para, horribly named "list_box"--but the para is only set when the onchcange event of the listbox fires, and the initial value of the para is set to nothing.
The way to get the initial value of the listbox when a button is clicked is by not relying on the onchange event. Instead, when the button is clicked, simply query the listbox for its current value, e.g. @my_listbox.text.
However, querying a listbox for its current value does not work directly inside the app block--apparently the listbox does not exist until after the app block has finished executing. In that case, you need to manually set the initial value of the list box:
@default_choice = "red"
@current_choice = "red"
list_box :items => ["blue", "red", "green"], :choose => @default_choice do |list|
@current_choice = list.text
end