class FFF < Wx::Frame
def initialize @b = Array.new
super(nil, 2, “Bljllll”, [5,5], [640,480]) @mypanel = Wx::Panel.new(self)
for i in 0…6 @b[i] = Wx::Button.new(@mypanel, i, “#{i}”, [10+25*i, 10],
[20,20])
evt_button(i){puts i}
end
end
end
Wx::App.run do
FFF.new.show
end
I’m create pre-condition number of buttons(7 in example above). What
I must do to set every button event which puts button id(btw, it’s equal
label of buttons too)? For example: if I print button “0”, it must puts
0, not 6 as a my code(in my code click to all buttons puts last number
in array, here it is 6).
Are there events to images such as click and mouse_on?
@b = Array.new
super(nil, 2, “Bljllll”, [5,5], [640,480]) @mypanel = Wx::Panel.new(self)
for i in 0…6 @b[i] = Wx::Button.new(@mypanel, i, “#{i}”, [10+25*i, 10],
[20,20])
evt_button(i){puts i}
evt_button(@b[i]) { puts i }
I’m create pre-condition number of buttons(7 in example above). What
I must do to set every button event which puts button id(btw, it’s equal
label of buttons too)? For example: if I print button “0”, it must puts
0, not 6 as a my code(in my code click to all buttons puts last number
in array, here it is 6).
This is answered in-line in the code.
Are there events to images such as click and mouse_on?
I believe the standard event handlers for StaticBitmap will work, as
it’s hierarchy is Object > EvtHandler > Window > Control > StaticBitmap.
Standard Images, through Wx::Image, Wx::Bitmap, and such, do not have
Events, as they are considered Memory Objects, not Actual Controls.
Basically, if you look at the docs for any class we have, and it has
somewhere in it’s hierarchy EvtHandler, then there are events that the
Class
will receive.
That’s the way to do it - it’s not very clean since it probably will be
triggered by any button event
but you can process only events starting with a specific value etc and
ignore the rest (and use more strict handlers):
class FFF < Wx::Frame
def initialize
@b = Array.new
super(nil, 2, "Bljllll", [5,5], [640,480])
@mypanel = Wx::Panel.new(self)
for i in 0..6
@b[i] = Wx::Button.new(@mypanel, i, "#{i}", [10+25*i, 10],
[20,20])
end
# here you specify global event handler which
# will process any button press
# this uses event delegation and works dynamically
# so it will works regardless of number of buttons
evt_button(Wx::ID_ANY){ |ev| puts ev.get_id }
end
end
Yes, there is evt_right_down, evt_right_up and evt_right_dclick (and
also similar events for the middle button).
Note that if you’re creating context menus (a common use for right
clicks) it is better to use evt_context_menu which abstracts away
platform differences in the means by which such a menu is summoned and
dismissed.
I tried to add evt_right_dclick, for example, but this is an error:
C:/Ruby/lib/ruby/gems/1.9.1/gems/wxruby-ruby19-2.0.1-x86-mingw32/lib/wx/classes/evthandler.rb:136:in
`acquire_handler’: Specify event handler with a method, name, proc OR
block (ArgumentError)
Another question: how can I make in Wx::Frame a scrollbar(vertical)? I
have big number of labels, that’s not fit into screen.
Maybe Wx::ScrolledWindow? Have a look at the samples as there are
several ways to use this class. The easiest way is with sizers where the
scrolling will be calculated automatically for you.
alex
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.