Evt_button + images_click

Hello.

I have two questions.

Here is my code:

require “wx”

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

  1. 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).

  2. Are there events to images such as click and mouse_on?

Hello Michael,

On Fri, Nov 12, 2010 at 5:24 PM, Misha O. [email protected]
wrote:

@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 }
  1. 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.

  1. 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.

hth,

Mario

  1. evt_button(@b[i]) { puts i } doesn’t work I need. It works like a
    evt_button(i){puts i}. I don’t know why.

Hi!

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

If you want clickable images you should use Wx::BitmapButton class:
http://wxruby.rubyforge.org/doc/bitmapbutton.html

Hope that helps :slight_smile:

Łukasz

Hello else one.

I decided a problem 1):

evt_button(i){|xx| puts xx.get_id}

Now working with problem 2

Misha O. wrote in post #961550:

Hello else one.

I decided a problem 1):

evt_button(i){|xx| puts xx.get_id}

Now working with problem 2

Did you try wx::BitmapButton?

Łukasz

Can I match the event “click right button of the mouse? (non left)”?

Hello. I will try BitmapButton some later.

Hi

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.

alex

Can you help me?

For example, I create the button:

b = Wx::Button.new(@mypanel, 10, “This is button”)
evt_button(10){puts “This is button-10”} # Here is event for left-click
mouse.

Can you write event for b, but with the right click?

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)

It is easy to do:
evt_button(@button) do
self.close
end

If that is the only window you have, the app should end on it’s own,
otherwise you will need to call Wx::App.exit_main_loop

hth,

Mario

Sent from my iPod

Else one question: how can I close the frame and stop the program?

And how can I run program on ruby?

Thanks.

Another question: how can I make in Wx::Frame a scrollbar(vertical)? I
have big number of labels, that’s not fit into screen.

On 23/11/10 22:32, Misha O. wrote:

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