# Button Event problem within Wizard

**URL:** <https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384>\
**Category:** wxRuby\
**Created:** [September 29, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384 "2009-09-29T22:24:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [September 29, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384/1 "2009-09-29T22:24:59Z")

</div>

In the code below my panel1\_impt button and panel1\_dnld buttons don’t  
trigger the button event. I’m banging my head against the table  
wondering what’s going on. Can anyone advise?

```
wizard = Wx::Wizard.new(self, :title => "Wizard")
wizard.icon = Wx::Icon.new('wizard.ico')
splash.destroy

##########################################################
# Panel1, Masthead selection
panel1 = Wx::WizardPageSimple.new(wizard)
panel1_txt = Wx::StaticText.new(panel1, :label => "Select Site")
panel1_lst = Wx::CheckListBox.new(panel1, :choices => files)
panel1_btns = Wx::Panel.new(panel1)
panel1_impt = Wx::Button.new(panel1_btns, :label => "Import")
panel1_dnld = Wx::Button.new(panel1_btns, :label => "Download")

panel1_btns.sizer = Wx::HBoxSizer.new
panel1_btns.sizer.add(panel1_impt, 0, Wx::ALL, 5)
panel1_btns.sizer.add(panel1_dnld, 0, Wx::ALL, 5)

panel1.sizer = Wx::VBoxSizer.new
panel1.sizer.add(panel1_txt, 0, Wx::ALL, 5)
panel1.sizer.add(panel1_lst, 1, Wx::ALL|Wx::GROW, 5)
panel1.sizer.add(panel1_btns, 0, Wx::ALL, 5)

files.each_index {|i| panel1_lst.check(i, true)}

# Panel1 Events
evt_button(panel1_impt) {p "IMPORT!"}
evt_button(panel1_dnld) {p "DOWNLOAD!"}
```

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [September 30, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384/2 "2009-09-30T13:07:49Z")

</div>

El Gato wrote:

> In the code below my panel1\_impt button and panel1\_dnld buttons don’t  
> trigger the button event. I’m banging my head against the table  
> wondering what’s going on. Can anyone advise?

> ```
> panel1_impt = Wx::Button.new(panel1_btns, :label => "Import")
> panel1_dnld = Wx::Button.new(panel1_btns, :label => "Download")
> 
> ```

> ```
> # Panel1 Events
> evt_button(panel1_impt) {p "IMPORT!"}
> evt_button(panel1_dnld) {p "DOWNLOAD!"}
> 
> ```

Please, if you’re going to post code, reduce it to the bare minimum that  
shows the problem, and make it runnable.

There is nothing wrong with your event handling code itself. Things to  
consider:

- are you calling Wizard#run\_wizard to show the panel?
- is output buffering on in the terminal in which you’re running the  
application - in which case the output might not show straight away?

alex

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [September 30, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384/3 "2009-09-30T15:20:57Z")

</div>

I guess the problem with reducing the code is that I have no idea what  
the problem is, so I’m not really sure what not to show – it’s possible  
that some non-button item is the culprit. I didn’t really think that my  
event code was bad. Here’s a full program that shows the issue.

#!/usr/bin/env rubyw

require ‘wx’

$stdout.sync = true

class AppWizard \< Wx::Frame  
def initialize(\*args)  
super

```
wizard = Wx::Wizard.new(self, :title => "App Wizard")

panel1 = Wx::WizardPageSimple.new(wizard)
panel1_txt = Wx::StaticText.new(panel1, :label => "Panel 1:")
panel1_lst = Wx::CheckListBox.new(panel1, :choices => ["one", 

```

“two”, “three”])

```
# This event never triggers when the button is clicked
panel1_impt = Wx::Button.new(panel1, :label => "Import")
evt_button(panel1_impt) {puts "You clicked the import button"}

panel1.sizer = Wx::VBoxSizer.new
panel1.sizer.add(panel1_txt, 0, Wx::ALL, 5)
panel1.sizer.add(panel1_lst, 1, Wx::ALL|Wx::GROW, 5)
panel1.sizer.add(panel1_impt, 0, Wx::ALL, 5)

wizard.set_page_size(Wx::Size.new(500,300))
wizard.run_wizard(panel1)

```

end  
end

class App \< Wx::App  
def on\_init  
frame = AppWizard.new  
frame.show(false)  
end  
end

app = App.new  
app.main\_loop()

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [September 30, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384/4 "2009-09-30T15:50:45Z")

</div>

El Gato wrote:

> I guess the problem with reducing the code is that I have no idea what  
> the problem is, so I’m not really sure what not to show – it’s possible  
> that some non-button item is the culprit. I didn’t really think that my  
> event code was bad. Here’s a full program that shows the issue.

> ```
> # This event never triggers when the button is clicked
> panel1_impt = Wx::Button.new(panel1, :label => "Import")
> evt_button(panel1_impt) {puts "You clicked the import button"}
> 
> ```

wizard.evt\_button(panel1\_impt) {puts “You clicked the import button”}

Does the trick for me.

best,

```
Peter.

```

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [September 30, 2009, 4:32pm UTC](https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384/5 "2009-09-30T16:32:07Z")

</div>

Peter Lane wrote:

> El Gato wrote:
> 
> > ```
> > # This event never triggers when the button is clicked
> > panel1_impt = Wx::Button.new(panel1, :label => "Import")
> > evt_button(panel1_impt) {puts "You clicked the import button"}
> > 
> > ```
> 
> wizard.evt\_button(panel1\_impt) {puts “You clicked the import button”}

Thanks Peter.

By way of explanation, this is because CommandEvents - those associated  
with standard controls like Button, CheckBox, ToggleButton etc - bubble  
upwards through the window hierarchy UNTIL they reach a top-level  
window. Each window has a chance to process it.

So in the example, the Button event gets offered to  
the Button (then) the Panel (then) the Wizard (a top-level window)

But in the original evt\_button was called on “self”, the Frame. The  
button event never reaches that Frame it so the event handler isn’t ever  
triggered. Calling wizard.evt\_button sets it on the Wizard instead.

Encapsulating GUI classes by inheritance tends to help avoid this mix-up  
(class DownloadWizard \< Wx::Wizard …)

hth  
alex

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [September 30, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/button-event-problem-within-wizard/176384/6 "2009-09-30T16:30:48Z")

</div>

Peter Lane wrote:

> wizard.evt\_button(panel1\_impt) {puts “You clicked the import button”}
> 
> Does the trick for me.
> 
> best,
> 
> ```
> Peter.
> 
> ```

It does indeed! Thanks very much!
