How to use menuitems

When I try to work with menus I get the following error message after
selecting “Red” from the colour menu. The following code produces the
following error message. What am I doing wrong and how do I fix it.
Thank you in advance.

  • Philip

EtestApp.rb:57:in do_Red': undefined method ===>check’<==== for
109:Fixnum (NoMethodError)
from EtestApp.rb:34:in `initialize’
(now line 125)

Etest.rb

This class was automatically generated from XRC source. It is not

recommended that this file is edited directly; instead, inherit from

this class and extend its behaviour there.

Source file: Etest.xrc

Generated at: Sun Oct 11 20:05:09 -0600 2009

class EyesFrame < Wx::Frame

attr_reader :m_statictext7, :m_textrednumber, :m_statictext9,
:m_textgreennumber, :m_statictext11, :m_textbluenumber,
:m_buttonchange, :m_menubar1, :m_menu1, :m_miexit,
:m_menucolour, :m_mired, :m_migreen, :m_miblue,
:m_miwhite

def initialize(parent = nil)
super()
xml = Wx::XmlResource.get
xml.flags = 2 # Wx::XRC_NO_SUBCLASSING
xml.init_all_handlers
xml.load(“Etest.xrc”)
xml.load_frame_subclass(self, parent, “MyFrame”)

finder = lambda do | x |
  int_id = Wx::xrcid(x)
  begin
    Wx::Window.find_window_by_id(int_id, self) || int_id
  # Temporary hack to work around regression in 1.9.2; remove
  # begin/rescue clause in later versions
  rescue RuntimeError
    int_id
  end
end

@m_statictext7 = finder.call("m_staticText7")
@m_textrednumber = finder.call("m_textRedNumber")
@m_statictext9 = finder.call("m_staticText9")
@m_textgreennumber = finder.call("m_textGreenNumber")
@m_statictext11 = finder.call("m_staticText11")
@m_textbluenumber = finder.call("m_textBlueNumber")
@m_buttonchange = finder.call("m_buttonchange")
@m_menubar1 = finder.call("m_menubar1")
@m_menu1 = finder.call("m_menu1")
@m_miexit = finder.call("m_miexit")
@m_menucolour = finder.call("m_menuColour")
@m_mired = finder.call("m_miRed")
@m_migreen = finder.call("m_miGreen")
@m_miblue = finder.call("m_miBlue")
@m_miwhite = finder.call("m_miwhite")
if self.class.method_defined? "on_init"
  self.on_init()
end

end
end

Etest.App.rb

begin
require ‘rubygems’
rescue LoadError
end
require ‘wx’
require ‘Etest.rb’

class AppFrame < EyesFrame

Inherit from the generated base class and set up event handlers

set up pen and brush colours

@red_value = 0
@green_value = 0
@blue_value = 0

Create Needed Brushes and Pens

BG_BRUSH = Wx::Brush.new
@fg_pen = Wx::Pen.new

@font = Wx::NULL_FONT
@text_colour = Wx::NULL_COLOUR

def initialize()
super()
@font = Wx::Font.new(50,Wx::FONTFAMILY_TELETYPE,
Wx::FONTSTYLE_NORMAL,Wx::FONTWEIGHT_NORMAL)
BG_BRUSH.set_colour(255,0, 0)
# Setup the actual data to be stored in Brushes and Pens

  do_rndValues
  evt_button(@m_buttonchange) { do_rndValues }
  evt_paint { on_paint }
  evt_menu(@m_mired) { do_Red}
  evt_menu(@m_migreen) { do_Green }
  evt_menu(@m_miblue) { do_Blue }
  evt_menu(@m_miwhite) { do_white }

end

def do_rndValues
@red_value = rand(255)
@green_value = rand(255)
@blue_value = rand(255)
@m_textrednumber.label = @red_value.to_s
@m_textgreennumber.label = @green_value.to_s
@m_textbluenumber.label = @blue_value.to_s
refresh()
end

def do_Red()
@red_value = 255
@green_value = 0
@blue_value = 0
@m_textrednumber.label = @red_value.to_s
@m_textgreennumber.label = @green_value.to_s
@m_textbluenumber.label = @blue_value.to_s
@m_mired.check(true)
@m_migreen.check(false)
@m_miblue.check(false)
@m_miwhite.check(false)

  refresh()

end

def on_paint
# We do our drawing now
rect = self.get_client_size
paint do |dc|
gdc = Wx::GraphicsContext.create(dc)

    # draw rectangle
    temp_brush = gdc.create_brush(BG_BRUSH)
    gdc.set_brush(temp_brush)

    x = 0
    y = 0
    w = 300
    h = 260
    gdc.draw_rectangle(x,y,w,h)
 end # paint do

end # on paint
end

Wx::App.run do
AppFrame.new.show
end

The problem that you are running into, is the fact that when dealing
with
XRC Files, the lookup method returns the Integer ID for the Menu Items,
instead of the Actual Instance of MenuItem class. What you need to do,
is
something along the lines of this:
self.get_menu_bar.find_item(@m_mired).check(true)

The same with the ones that you want to uncheck.

hth,

Mario

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs