# Wx Ruby - MenuBar. What's wrong with my code?

**URL:** <https://www.ruby-forum.com/t/wx-ruby-menubar-whats-wrong-with-my-code/197197>\
**Category:** Ruby\
**Created:** [May 15, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/wx-ruby-menubar-whats-wrong-with-my-code/197197 "2010-05-15T17:59:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 15, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/wx-ruby-menubar-whats-wrong-with-my-code/197197/1 "2010-05-15T17:59:30Z")

</div>

Hi all,

I have created a basic frame and I’m trying to create a MenuBar inside  
it but although I have followed the API Documentation instructions I  
always get this error.

This is my code

require ‘wx’  
include Wx

class MyFrame \< Wx::Frame  
def initialize  
super(nil,-1,‘MinApp’)

```
@panel = Wx::Panel.new(self)
@menu_bar = Wx::MenuBar.new(
  2,
  ['File','Edit','Help'],
  ['Open','Save','Exit'],
  0)
show

```

end  
end

class MinApp \< App  
def on\_init  
frame = MyFrame.new  
end  
end

MinApp.new.main\_loop

And this is the error:

C:/Documentos/Ruby Scripts/Frame\_Ivo/lib/Ivo.rb:13:in `initialize': wrong # of arguments(4 for 0) (ArgumentError) from C:/Documentos/Ruby Scripts/Frame_Ivo/lib/Ivo.rb:13:in`new’  
from C:/Documentos/Ruby Scripts/Frame\_Ivo/lib/Ivo.rb:13:in  
`initialize' from C:/Documentos/Ruby Scripts/Frame_Ivo/lib/Ivo.rb:31:in`new’  
from C:/Documentos/Ruby Scripts/Frame\_Ivo/lib/Ivo.rb:31:in `on_init' from C:/Documentos/Ruby Scripts/Frame_Ivo/lib/Ivo.rb:35:in`main\_loop’  
from C:/Documentos/Ruby Scripts/Frame\_Ivo/lib/Ivo.rb:35:in `’

Thanks in advance for any help

Ivo R.

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 15, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/wx-ruby-menubar-whats-wrong-with-my-code/197197/2 "2010-05-15T18:07:45Z")

</div>

On Sat, May 15, 2010 at 12:59 PM, Ivo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> include Wx  
> [‘Open’,‘Save’,‘Exit’],  
> end  
> from C:/Documentos/Ruby Scripts/Frame\_Ivo/lib/Ivo.rb:31:in `new’

Hi Ivo, the constructor for Wx::MenuBar doesn’t accept any arguments,  
you  
have to write you code in another way to achieve what you want.

Thiago Brandao

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 15, 2010, 6:58pm UTC](https://www.ruby-forum.com/t/wx-ruby-menubar-whats-wrong-with-my-code/197197/3 "2010-05-15T18:58:37Z")

</div>

thiago damasceno wrote:

> On Sat, May 15, 2010 at 12:59 PM, Ivo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > include Wx  
> > [‘Open’,‘Save’,‘Exit’],  
> > end  
> > from C:/Documentos/Ruby Scripts/Frame\_Ivo/lib/Ivo.rb:31:in `new’
> 
> Hi Ivo, the constructor for Wx::MenuBar doesn’t accept any arguments,  
> you  
> have to write you code in another way to achieve what you want.
> 
> Thiago Brandao

Hi Tiago,

If the Wx::MenuBar doesn’t accept arguments then I will have to create a  
Wx::Menu inside the MenuBar with all the items that I want. Is this  
correct?

Thanks

Ivo R.

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 17, 2010, 2:30am UTC](https://www.ruby-forum.com/t/wx-ruby-menubar-whats-wrong-with-my-code/197197/4 "2010-05-17T02:30:00Z")

</div>

Marvin GÃ¼lker,

That’s exactly what I’m looking for…

Thank you

Ivo R.

---

<div class="post-metadata">

**Author:** ![iroupa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@iroupa](https://www.ruby-forum.com/u/iroupa)\
**Post date:** [May 15, 2010, 7:42pm UTC](https://www.ruby-forum.com/t/wx-ruby-menubar-whats-wrong-with-my-code/197197/5 "2010-05-15T19:42:19Z")

</div>

Ivo R. wrote:

> If the Wx::MenuBar doesn’t accept arguments then I will have to create a  
> Wx::Menu inside the MenuBar with all the items that I want. Is this  
> correct?

## I’m not sure about what you mean by that, but have a look at this minimal example (tested with Ruby 1.9):

#!/usr/bin/env ruby  
#Encoding: UTF-8  
require “wx”

class MyFrame \< Wx::Frame  
include Wx

def initialize(parent = nil)  
super(parent, :size =\> Size.new(400, 400), :title =\> “Test”)

```
#Just to fill that empty window
StaticText.new(self, :label => "This is a test GUI.")
Button.new(self, :label => "This is a test button.", :pos => 

```

Point.new(20, 20))

```
#1. Create the MenuBar.
menu_bar = MenuBar.new

#2. Create a menu you want to append to the menu bar.
menu = Menu.new
#3. Create menu entries. You could place submenus here to.
menu.append(ID_PREFERENCES, "Preferences...")
#This is a horizontal line.
menu.append_separator
#Yet another item
menu.append(ID_EXIT, "Quit")

#4. Now append the menu to the menu bar. The ampersand
#sign & indicates that this menu will be accessible via
#[ALT] + [F], i.e. [ALT] + [<next letter>].
menu_bar.append(menu, "&File")

#5. Set the frame's menu bar to our newly created one.
self.menu_bar = menu_bar

#Process the menu bar's events.
evt_menu(ID_PREFERENCES) do |event|
  md = MessageDialog.new(self, :caption => "Testpreferences", 

```

:message =\> “Your preferences dialog goes here.”, :style =\> OK |  
ICON\_INFORMATION)  
md.show\_modal  
end

```
evt_menu(ID_EXIT){|event| self.close}

```

end

end

## Wx::App.run do mainwindow = MyFrame.new mainwindow.show end

You may also have a look at bigdemo in the wxRuby samples directory  
(look where you install your gems to) and at the wxRuby forum at  
[http://www.ruby-forum.com/forum/36](http://www.ruby-forum.com/forum/36).

Marvin
