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:
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