I''m trying to setup a fairly simple GUI with Ruby to perform some SQL queries. There are a few drop-down boxes where users can select different options and then one button that when clicked should get the text from each drop-down in order to perform the query. Once it gets the result it should display it to a text control. The problem is I am not sure how to actually reference each drop-down box in the frame or the text control. If I add the control directly to the frame there is no reference or name for that control that my button can later use. I have tried looking through sample code but most of the samples I found were for larger programs with many features and I couldn''t really find the answer I was looking for. Could someone give me an idea of how to accomplish this or maybe a link to some reference that may help? It doesn''t seem like it would be that difficult but I''m just not sure how to get started. Thanks, Alex Ciarlillo
Philip Schalm
2007-Mar-30 04:08 UTC
[Wxruby-users] object-oriented GUI design and event handling
Hi Alex, Are you doing this with WxSugar or solely with WxRuby2? If you''re using WxSugar you could do something like the following: # Begin Code require ''wx'' require ''wx_sugar'' class MainFrame < Wx::Frame def initialize(*args) super @buttons = [] @buttons << add( Wx::Choice[ :choices => ("1".."5").to_a ] ) @buttons << add( Wx::Choice[ :choices => ("a".."e").to_a ] ) @buttons << add( Wx::Choice[ :choices => ("F".."J").to_a ] ) add( Wx::Button[ :label => ''Do Something''] ) do |button| listen(:button,button) do |event| @textctrl.value = @buttons.map{|but|but.string_selection}.join('','') end end @textctrl = add( Wx::TextCtrl[] ) end end class MyApp < Wx::App def on_init MainFrame.new( nil, :title => "SQL Application" ).show end end MyApp.new.main_loop # End Code As for plain WxRuby, it''s a little more involved but will follow basically the same principles (I''d recommend WxSugar - it makes things much more rubyish) Cheers On 3/29/07, Alex Ciarlillo <ac251404 at ohio.edu> wrote:> I''m trying to setup a fairly simple GUI with Ruby to perform some SQL > ... > to get started. > > Thanks, > Alex Ciarlillo > _______________________________________________ > wxruby-users mailing list > wxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/wxruby-users >-- -Phil <><
Alex Fenton
2007-Mar-30 04:20 UTC
[Wxruby-users] object-oriented GUI design and event handling
Hi Alex Alex Ciarlillo wrote:> I''m trying to setup a fairly simple GUI with Ruby to perform some SQL > queries. There are a few drop-down boxes where users can select > different options and then one button that when clicked should get the > text from each drop-down in order to perform the query. Once it gets the > result it should display it to a text control. The problem is I am not > sure how to actually reference each drop-down box in the frame or the > text control.For most simpler apps, the easiest way to reference widgets (e.g. a drop-down box) is to store it as a ruby instance variable of the containing frame when you create it. Then you can refer to them later in your event handler, either directly through an instance variable, or by ordinary ruby accessor methods. The latter is slightly more work, but as generally in ruby helps avoid typo errors and documents your frame class. A simple example using wxSugar might look something like: class SimpleFrame < Wx::Frame attr_reader :colour_ch, :size_ch, :results def initialize(parent) super(parent, :title => ''Database App'') # set up some drop downs, an action button and a results display panel = add(Wx::Panel, :proportion => 1) panel.arrange_vertically(:padding => 8) do colours = %w[red blue green orange black] @colour_ch = panel.add( Wx::Choice[:choices => colours]) sizes = %w[small medium large] @size_ch = panel.add( Wx::Choice[:choices => sizes]) panel.add( Wx::Button[:label => ''Find'' ] ) do | @button | listen(:button, @button, :on_find) end @results = panel.add( Wx::TextCtrl[:style => Wx::TE_MULTILINE], :proportion => 1) end end def on_find what_colour = colour_ch.string( colour_ch.selection ) what_size = size_ch.string( size_ch.selection ) # Do retrieval from the database here ... results.value = "Results for ''#{what_colour}, #{what_size}'' ..." end end hth alex
Alex Ciarlillo
2007-Mar-30 19:39 UTC
[Wxruby-users] object-oriented GUI design and event handling
Thanks to both of you for getting me on the right track with this. Everything is coming together fine now except a problem I am having with CheckListBox. I would like the user to be able to select multiple items from certain lists so I thought this control would work, but now I can''t figure out how to get at the selections. I changed colour_ch in Alex''s example to a checklistbox but doing colour_ch.selections returns nothing. I read in the wxRuby docs that you are supposed to pass an array to be filled when using ListBox.get_selections but when I try that it gives me an error that I passed 1 argument where 0 is expected. I thought I would be able to do something like the following: what_colour = colour_ch.selections.map{|s| colour_ch.string(s)}.join('','') So that''s my guess and even though its wrong I believe its clear that I''m just trying to get a comma seperated string of all the items selected in that control. How should I be attempting this? Thanks, -alex c.
Philip Schalm
2007-Mar-30 20:44 UTC
[Wxruby-users] object-oriented GUI design and event handling
Hi Alex C, Wx::ChecklistBox#selections() won''t return the checked items - it will return the items that are selected (if you''re on Windows, the ones that are highlighted blue in the control). To get a list of the checked items, do something like the following: what_colour = (0..(colour_ch.number)).to_a.map{ |n| colour_ch.string(n) }.join('','') (not sure if that''s the exact syntax, but it should be close). As well, I''m not sure, but WxSugar might let you do something like the following: what_colour = colour_ch.each { |index| colour_ch.string(index) }.join('','') And, if that doesn''t work right now, would that be something useful to add to WxSugar, Alex F? On 3/30/07, Alex Ciarlillo <ac251404 at ohio.edu> wrote:> Thanks to both of you for getting me on the right track with this. > Everything is coming together fine now except a problem I am having with > CheckListBox. I would like the user to be able to select multiple items > from certain lists so I thought this control would work, but now I can''t > figure out how to get at the selections. I changed colour_ch in Alex''s > example to a checklistbox but doing colour_ch.selections returns > nothing. I read in the wxRuby docs that you are supposed to pass an > array to be filled when using ListBox.get_selections but when I try that > it gives me an error that I passed 1 argument where 0 is expected. I > thought I would be able to do something like the following: > > what_colour = colour_ch.selections.map{|s| colour_ch.string(s)}.join('','') > > So that''s my guess and even though its wrong I believe its clear that > I''m just trying to get a comma seperated string of all the items > selected in that control. How should I be attempting this? > > Thanks, > -alex c. > _______________________________________________ > wxruby-users mailing list > wxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/wxruby-users >-- -Phil <><
Philip Schalm
2007-Mar-30 20:59 UTC
[Wxruby-users] object-oriented GUI design and event handling
I made an oopsie (that''s in the documentation) ... Wx::ControlWithItems#number is not defined, you need to use Wx::ControlWithItems#count instead, so the first example should be: what_colour = (0..(colour_ch.count)).to_a.map{ |n| colour_ch.string(n) }.join('','') On 3/30/07, Philip Schalm <pnomolos at gmail.com> wrote:> Hi Alex C, > > Wx::ChecklistBox#selections() won''t return the checked items - it will > return the items that are selected (if you''re on Windows, the ones > that are highlighted blue in the control). To get a list of the > checked items, do something like the following: > > what_colour = (0..(colour_ch.number)).to_a.map{ |n| > colour_ch.string(n) }.join('','') > > (not sure if that''s the exact syntax, but it should be close). > As well, I''m not sure, but WxSugar might let you do something like the > following: > > what_colour = colour_ch.each { |index| colour_ch.string(index) }.join('','') > > And, if that doesn''t work right now, would that be something useful to > add to WxSugar, Alex F? > > On 3/30/07, Alex Ciarlillo <ac251404 at ohio.edu> wrote: > > Thanks to both of you for getting me on the right track with this. > > Everything is coming together fine now except a problem I am having with > > CheckListBox. I would like the user to be able to select multiple items > > from certain lists so I thought this control would work, but now I can''t > > figure out how to get at the selections. I changed colour_ch in Alex''s > > example to a checklistbox but doing colour_ch.selections returns > > nothing. I read in the wxRuby docs that you are supposed to pass an > > array to be filled when using ListBox.get_selections but when I try that > > it gives me an error that I passed 1 argument where 0 is expected. I > > thought I would be able to do something like the following: > > > > what_colour = colour_ch.selections.map{|s| colour_ch.string(s)}.join('','') > > > > So that''s my guess and even though its wrong I believe its clear that > > I''m just trying to get a comma seperated string of all the items > > selected in that control. How should I be attempting this? > > > > Thanks, > > -alex c. > > _______________________________________________ > > wxruby-users mailing list > > wxruby-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/wxruby-users > > > > > -- > -Phil <>< >-- -Phil <><
Alex Fenton
2007-Mar-31 20:49 UTC
[Wxruby-users] object-oriented GUI design and event handling
Alex Ciarlillo wrote:> Everything is coming together fine now except a problem I am having with > CheckListBox. I would like the user to be able to select multiple items > from certain lists so I thought this control would work, but now I can''t > figure out how to get at the selections.....> what_colour = colour_ch.selections.map{|s| colour_ch.string(s)}.join('','') >This is right. I''ve tried it out myself and I think there''s a bug in CheckListBox - the method always seems to return [0]. This is a bit strange because the same method works fine in ListBox where the method is inherited from. Anyway I will look into it, hopefully can fix it pretty easily for the next release. I guess you could use ListBox in the interim - samples/bigdemo/wxListBox in the main wxruby2 dist illustrates how to use get_selections. Thanks for the report. alex
Alex Fenton
2007-Mar-31 21:07 UTC
[Wxruby-users] object-oriented GUI design and event handling
Philip Schalm wrote:> Wx::ChecklistBox#selections() won''t return the checked items - it will > return the items that are selected (if you''re on Windows, the ones > that are highlighted blue in the control). To get a list of the > checked items, do something like the following: > > what_colour = (0..(colour_ch.number)).to_a.map{ |n| > colour_ch.string(n) }.join('','') >Oops, I should have read this email first. Alex - ignore my previous email, Philip''s quite right that get_selections doesn''t return the checked items. The sort of thing you want is * items = (0 .. count).to_a checked = items.select { | i | checked?(i) }.map { | i | string(i) This works fine for me. *No bug ... I must be paranoid.> As well, I''m not sure, but WxSugar might let you do something like the > following: > > what_colour = colour_ch.each { |index| colour_ch.string(index) }.join('','') > > And, if that doesn''t work right now, would that be something useful to > add to WxSugar, Alex F? >yes - I''ve been experimenting with some methods to improve working with ControlWithItems classes, but they didn''t make it in the last release. We''ll look to put it in the next one. thanks a