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 =>200do defconvert(temp,unit='C') ifunit =="C" (temp.to_i *9.0/5.0)+32.0 elsifunit =="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 maybe 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.
Hi Jim, Thank you for the post. :) > 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. Yeah, I confirmed the same behavior with Shoes 3 for Windows. > This makes me think my method is not getting it's value from > the list_box. Ah,... what does "not getting it's value" mean? ;-) Until you click the list_box, the block of list_box will not be executed. So, `@unit.text` will get nothing. But what does the "nothing" mean? In this case, @unit.text's value is '' (empty string), not nil. And in Ruby, '' != nil. Hence, even if you set 'C' as the default value, the default value is not assigned to the local variable `unit`. Hope this helps, ashbb
Thanks for clearing up why it didn't work ashbb. It's just as good to know why something works as knowing why it doesn't. ________________________________ From: ashbb <ashbbb@gmail.com> To: shoes@librelist.org Sent: Tuesday, 31 January 2012 7:46 PM Subject: Re: [shoes] list_box woes Hi Jim, Thank you for the post. :) > 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. Yeah, I confirmed the same behavior with Shoes 3 for Windows. > This makes me think my method is not getting it's value from > the list_box. Ah,... what does "not getting it's value" mean? ;-) Until you click the list_box, the block of list_box will not be executed. So, `@unit.text` will get nothing. But what does the "nothing" mean? In this case, @unit.text's value is '' (empty string), not nil. And in Ruby, '' != nil. Hence, even if you set 'C' as the default value, the default value is not assigned to the local variable `unit`. Hope this helps, ashbb
hi Jim, hmm - you're script works as i would expect it to. when i run it, and enter "20" into the edit_line, and then click the "Compute" button - i get a nice number 68 returned to me, which seems reasonable. i think i'm not quite sure what you're trying to do exactly... When I start the program and enter a number it does nothing... This makes > me think my method is not getting it's value from the list_box. > right, until you click the button you don't do any computing. if you want this to work as you type - without having to click the "Compute" button - you could do something like this: Shoes.app :title=> 'Temperature Converter', :width => 200, :height => 200 do def convert(temp, unit='C') if unit == "C" ((temp.to_f * 9.0 / 5.0) + 32.0).round(2) elsif unit == "F" ((temp.to_f - 32.0) * (5.0 / 9.0)).round(2) end end list_box :items => ["C", "F"], :choose => "C" do |item| @unit = item.text end line1 = edit_line :width => 100 line1.change do @result.text = convert(line1.text, @unit) end @result = para end > I tried setting 'C' as the default variable... > and you did - it's just that "C" doesn't have any value assigned to it at startup. > Also, and maybe unrelated, if I remove '@unit = para' from the end it > won't print anything, even the @result. Why is that? > if you remove @unit = para, you'll get an error in the line where you say "@unit.text = item.text", as @unit is undefined. notice that you don't need to define @unit as a `para`, since it's something that you're never going to display onscreen (unless that's what you intended....) you can just have @unit be an instance variable (with the value of the edit_line text assigned to it) that you can pass around between methods. also, i changed your #.to_i's to #.to_f's (to floats) because you are doing math with floats and not integers - you could use #.to_i, but then it would be useless to multiply by (5.0 / 9.0) etc., you could just use (5 / 9) keep at it - Shoes (like anything) can be kind of funny until you get the hang of it - but it seems like you're getting that hang ;) Shoes On! - j
Hi J, > hmm - you're script works as i would expect it to. when i run it, > and enter "20" into the edit_line, and then click the "Compute" button > - i get a nice number 68 returned to me, which seems reasonable. Umm,... really? Try out this code again: http://pastebin.ca/2107977 I think the above code is his original code. On my Windows 7 and Ruby 1.9.3p0, when I start the program and enter "20" and click the "Compute" button, then nothing happens. ashbb
hey ash - well, i don't know why, but both the post here and the pastebin code work for me as they are on ubuntu 10.04 - strange... i wonder if it has to do with how i execute the code... i use scite, and have set the `run` option to `~/shoes/dist/shoes $(FileNameExt)`... i noticed with my player that sometimes i could launch it through scite or through the command line, but *not* by choosing the `open an app` option after starting up shoes - not sure what that's all about... @Jim - glad to hear it's working - Shoes On! - j On Tue, Jan 31, 2012 at 11:48 AM, ashbb <ashbbb@gmail.com> wrote: > Hi J, > > > > hmm - you're script works as i would expect it to. when i run it, > > and enter "20" into the edit_line, and then click the "Compute" button > > - i get a nice number 68 returned to me, which seems reasonable. > Umm,... really? > > Try out this code again: http://pastebin.ca/2107977 > > I think the above code is his original code. > On my Windows 7 and Ruby 1.9.3p0, when I start the program and enter "20" > and click the "Compute" button, then nothing happens. > > ashbb
Hi J, > both the post here and the pastebin code work for me as they are > on ubuntu 10.04 Umm,... strange... There may be a difference between Linux and Windows. Could you try to run the following snippet? Shoes.app do msg = para para(msg.text == nil) end On my Windows, the 'false' is displayed. ashbb
hi ash,. > There may be a difference between Linux and Windows. > Could you try to run the following snippet? > > Shoes.app do > msg = para > para(msg.text == nil) > end > > On my Windows, the 'false' is displayed. ...confirmed - i get the same... but i think there are some important differences - still trying to figure out exactly what they are! - j
Thanks for your help Kaiden, but it's still not working for me! When I start it up, put in a number and then press convert, it doesn't display any calculation. But if I then select something from the list_box and press convert it works fine. So I'm not sure what's going on. I thought maybe I had an old version of Shoes or something so I built a new one from github but that didn't solve the problem. Is there a way I can force the list_box to send the value on startup somehow? Thanks for explaining the '@unit' mystery and picking up my 'to_i' mistake! ________________________________ From: J. Kaiden <jakekaiden@gmail.com> To: shoes@librelist.org Sent: Tuesday, 31 January 2012 10:16 AM Subject: Re: [shoes] list_box woes hi Jim, hmm - you're script works as i would expect it to. when i run it, and enter "20" into the edit_line, and then click the "Compute" button - i get a nice number 68 returned to me, which seems reasonable. i think i'm not quite sure what you're trying to do exactly... When I start the program and enter a number it does nothing... This makes me think my method is not getting it's value from the list_box. right, until you click the button you don't do any computing. if you want this to work as you type - without having to click the "Compute" button - you could do something like this: Shoes.app :title=> 'Temperature Converter', :width => 200, :height => 200 do def convert(temp, unit='C') if unit == "C" ((temp.to_f * 9.0 / 5.0) + 32.0).round(2) elsif unit == "F" ((temp.to_f - 32.0) * (5.0 / 9.0)).round(2) end end list_box :items => ["C", "F"], :choose => "C" do |item| @unit = item.text end line1 = edit_line :width => 100 line1.change do @result.text = convert(line1.text, @unit) end @result = para end I tried setting 'C' as the default variable... and you did - it's just that "C" doesn't have any value assigned to it at startup. Also, and maybe unrelated, if I remove '@unit = para' from the end it won't print anything, even the @result. Why is that? if you remove @unit = para, you'll get an error in the line where you say "@unit.text = item.text", as @unit is undefined. notice that you don't need to define @unit as a `para`, since it's something that you're never going to display onscreen (unless that's what you intended....) you can just have @unit be an instance variable (with the value of the edit_line text assigned to it) that you can pass around between methods. also, i changed your #.to_i's to #.to_f's (to floats) because you are doing math with floats and not integers - you could use #.to_i, but then it would be useless to multiply by (5.0 / 9.0) etc., you could just use (5 / 9) keep at it - Shoes (like anything) can be kind of funny until you get the hang of it - but it seems like you're getting that hang ;) Shoes On! - j
Scratch that, I got it working! Shoes.app :title=> 'Temperature Converter', :width => 200, :height => 200 do def convert(temp, unit='C') if unit == "C" ((temp.to_f * 9.0 / 5.0) + 32.0).round(2) elsif unit == "F" ((temp.to_f - 32.0) * (5.0 / 9.0)).round(2) end end @list_box = list_box :items => ["C", "F"], :choose => "C" do |item| @unit.text = unit end line1 = edit_line :width => 100 button 'Convert' do @result.text = convert(line1.text, @list_box.text) end @result = para end Now I've got my Shoes on haha! ________________________________ From: Jim Hendi <facetoe@ymail.com> To: shoes@librelist.org Sent: Tuesday, 31 January 2012 12:03 PM Subject: Re: [shoes] list_box woes Thanks for your help Kaiden, but it's still not working for me! When I start it up, put in a number and then press convert, it doesn't display any calculation. But if I then select something from the list_box and press convert it works fine. So I'm not sure what's going on. I thought maybe I had an old version of Shoes or something so I built a new one from github but that didn't solve the problem. Is there a way I can force the list_box to send the value on startup somehow? Thanks for explaining the '@unit' mystery and picking up my 'to_i' mistake! ________________________________ From: J. Kaiden <jakekaiden@gmail.com> To: shoes@librelist.org Sent: Tuesday, 31 January 2012 10:16 AM Subject: Re: [shoes] list_box woes hi Jim, hmm - you're script works as i would expect it to. when i run it, and enter "20" into the edit_line, and then click the "Compute" button - i get a nice number 68 returned to me, which seems reasonable. i think i'm not quite sure what you're trying to do exactly... When I start the program and enter a number it does nothing... This makes me think my method is not getting it's value from the list_box. right, until you click the button you don't do any computing. if you want this to work as you type - without having to click the "Compute" button - you could do something like this: Shoes.app :title=> 'Temperature Converter', :width => 200, :height => 200 do def convert(temp, unit='C') if unit == "C" ((temp.to_f * 9.0 / 5.0) + 32.0).round(2) elsif unit == "F" ((temp.to_f - 32.0) * (5.0 / 9.0)).round(2) end end list_box :items => ["C", "F"], :choose => "C" do |item| @unit = item.text end line1 = edit_line :width => 100 line1.change do @result.text = convert(line1.text, @unit) end @result = para end I tried setting 'C' as the default variable... and you did - it's just that "C" doesn't have any value assigned to it at startup. Also, and maybe unrelated, if I remove '@unit = para' from the end it won't print anything, even the @result. Why is that? if you remove @unit = para, you'll get an error in the line where you say "@unit.text = item.text", as @unit is undefined. notice that you don't need to define @unit as a `para`, since it's something that you're never going to display onscreen (unless that's what you intended....) you can just have @unit be an instance variable (with the value of the edit_line text assigned to it) that you can pass around between methods. also, i changed your #.to_i's to #.to_f's (to floats) because you are doing math with floats and not integers - you could use #.to_i, but then it would be useless to multiply by (5.0 / 9.0) etc., you could just use (5 / 9) keep at it - Shoes (like anything) can be kind of funny until you get the hang of it - but it seems like you're getting that hang ;) Shoes On! - j
Don't forget the Shoes Console when trying to figure out why things go wrong. alt-/ or control-/!