Mario S. wrote:
On Sun, Nov 22, 2009 at 4:58 PM, Gary H. [email protected]
wrote:
The “App” file ‘requires’ the “Gui” file.
stating that ‘choices’ in not a defined method.
variations of this approach.
My questions are:
- Why can’t I find anything within Wx::RadioBox that references
:choices?
To my knowledge, there is no :choices method on Wx::RadioBox at all. So
why
it works in your self-coded one, and not in XRC one, I can’t explain,
unless
you created the choices method yourself.
- Is there a way to add a new method to RadioBox from within the “App”
program when using the XRC approach?
Yes, there is a way to add new methods to a XRCized created class. You
need
to require the generated file first, before you append methods to the
class,
simply for the fact that if you create a class, before it’s defined, you
can’t subclass it, and the subclassing is already done in the XRCized
generated output.
The Correct Way:
require ‘my_xrc_generated_file’
class MyXrcWindow
def new_method
puts “Hello World from New Method!”
end
end
Keep in mind, that with the current version of XRCise, that if you need
to
define anything to be executed, when the class is created, use the
method
on_init.
EG: If you do:
require ‘my_xrc_generated_file’
class MyXrcWindow
def on_init
puts “This will be fired when an instance is created.”
@@instance_number |= 0
@@instance_number += 1
end
end
hth,
Mario
Thanks
Thanks for your help Mario!
Your examples helped me get my program working, and helped my
understanding.
As for there being no :choices within wx::RadioBox, there are two places
that show it. The first is in the official wxRuby documentation, listed
under wx::RadioBox, as follows:
RadioBox.new(Window parent, Integer id, String label,
Point point = DEFAULT_POSITION,
Size size = DEFAULT_SIZE,
Array choices =[],
Integer major_dimension = 0,
Integer style = RA_SPECIFY_COLS,
Validator validator = DEFAULT_VALIDATOR,
String name = “radioBox”)
Constructor, creating and showing a radiobox.
Parameters
* parent Parent window. Must not be nil.
* id Window identifier. A value of -1 indicates a default value.
* label Label for the static box surrounding the radio buttons.
* pos Window position. If the position (-1, -1) is specified then a
default position is chosen.
* size Window size. If the default size (-1, -1) is specified then a
default size is chosen.
* choices An array of String choices with which to initialize the
radiobox.
* major_dimension Specifies the maximum number of rows (if style
contains RA_SPECIFY_ROWS) or columns (if style contains RA_SPECIFY_COLS)
for a two-dimensional radiobox.
* style Window style. See RadioBox.
* validator Window validator.
* name Window name.
The second place is in the tutorial below, which was the non-XRC program
that I was referring to in my original post. See the following excerpt
from that tutorial:
WxRubyWiki__Buttons tutorial contains the following:
Working with radio buttons
#March 29, 2009
wxruby 2.0.0
Ruby 1.8.6
#mac osx 10.4.11
#7stud
require “rubygems”
require “wx”
class MyFrame < Wx::Frame
def initialize
super(nil, #Parent
:title => “RadioBox Example”, #Displays on top of window
:pos => [150, 25], #or Wx::DEFAULT_POSITION
:size => [300, 200] #or Wx::DEFAULT_SIZE
#For the position and size arguments, you don’t need to specify
Point and Size
#objects anymore. See: wxRuby Overview on the doc page
wxruby_intro.html
)
panel = Wx::Panel.new(self) #Parent = self = this Frame
drink_choices = [“coffee”, “tea”, “juice”, “milk”] #labels for
radio buttons
radios = Wx::RadioBox.new(
panel, #Parent
:label => “Drinks”, #Label for box surrounding radio
buttons
:pos => [20, 5],
:size => Wx::DEFAULT_SIZE,
:choices => drink_choices, #The labels for the radio buttons
:major_dimension => 1, #Max number of cols(or rows)–try
changing it to 2
:style => Wx::RA_SPECIFY_COLS #:major_dimension value applies to
columns
#The :major_dimension and :style in combination determine the
layout of the
#RadioBox. In this case, the maximum number of columns is 1,
which limits
#the number of radio buttons to one per line. Because there are
4 radio buttons,
#that means there will be 4 lines, with one radio button per
line.
)
evt_radiobox(radios.get_id()) {|cmd_event|
on_change_radio(cmd_event)}
#When there is an event of type evt_radiobox on the widget whose id
is radios.get_id(),
#the block is called and an “event object” is passed to the block.
The block calls
#the method on_change_radio(), defined below, relaying the event
object to the
#method. The event object contains useful information about the
radio button
#that was selected.
@text_widget = Wx::StaticText.new(
panel, #Parent
:label => “coffee”,
:pos => [150, 25],
:size => Wx::DEFAULT_SIZE
#Store a widget in an instance variable if other
#methods, like the one below, need access to it.
)
show #equivalent to self.show, makes the frame visible
end
def on_change_radio(cmd_event)
selected_drink = cmd_event.string #Selected radio’s label
#Instead of calling methods like cmd_event.get_string() or
cmd_event.set_string() you
#can now call accessor methods with the same name as the property
you are trying to
#get or set. See: wxRuby Overview on the doc page wxruby_intro.html
@text_widget.label = selected_drink
end
end
class MinimalApp < Wx::App
def on_init
MyFrame.new
end
end
MinimalApp.new.main_loop #main_loop() tells wxruby to start reacting
#to events on your App, like radio button
selections,
#button clicks, etc.
So it seems that those ‘choices’ are arguments without associated
methods. I was unable to get ‘choices’ listed as an argument. I only
found ‘choices’ listed as an argument in an error message, where I had
changed ‘choices’ to another word.
By the way, what are your examples supposed to do?
@@instance_number |= 0
@@instance_number += 1
It seems to me that the first one creates a variable with a value of
true.
Executing the second one then generates an error, since it can’t
increment true.
Gary