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.
On Sat, May 15, 2010 at 12:59 PM, Ivo R. [email protected] 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
thiago damasceno wrote:
On Sat, May 15, 2010 at 12:59 PM, Ivo R. [email protected] 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.
Marvin Gülker,
That’s exactly what I’m looking for…
Thank you
Ivo R.
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.
Marvin