# Evt\_button + images\_click

**URL:** <https://www.ruby-forum.com/t/evt-button-images-click/199046>\
**Category:** wxRuby\
**Created:** [November 12, 2010, 11:24pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046 "2010-11-12T23:24:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 12, 2010, 11:24pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/1 "2010-11-12T23:24:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 13, 2010, 8:11am UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/2 "2010-11-13T08:11:34Z")

</div>

Hello Michael,

On Fri, Nov 12, 2010 at 5:24 PM, Misha O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 13, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/3 "2010-11-13T13:39:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 13, 2010, 9:56pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/4 "2010-11-13T21:56:33Z")

</div>

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](http://wxruby.rubyforge.org/doc/bitmapbutton.html)

Hope that helps 🙂

Łukasz

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 15, 2010, 2:00pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/5 "2010-11-15T14:00:48Z")

</div>

Hello else one.

I decided a problem 1):

evt\_button(i){|xx| puts xx.get\_id}

Now working with problem 2

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 15, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/6 "2010-11-15T14:28:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 15, 2010, 7:00pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/7 "2010-11-15T19:00:29Z")

</div>

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

Hello. I will try BitmapButton some later.

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 16, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/8 "2010-11-16T14:03:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 18, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/9 "2010-11-18T20:15:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 19, 2010, 1:33am UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/10 "2010-11-19T01:33:44Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 21, 2010, 2:01am UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/11 "2010-11-21T02:01:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 20, 2010, 7:01pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/12 "2010-11-20T19:01:58Z")

</div>

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

And how can I run program on ruby?

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 23, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/13 "2010-11-23T23:32:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rarstd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rarstd](https://www.ruby-forum.com/u/rarstd)\
**Post date:** [November 25, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/evt-button-images-click/199046/14 "2010-11-25T16:33:19Z")

</div>

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
