Forum: Ruby gtk container add/remove malfunction

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kevin R. (Guest)
on 2009-04-22 00:33
i'm trying to remove and add a widget to a container when an event
occur. removing and adding works outside the event, but only removing
works within

list = Gtk::ComboBox.new

list.signal_connect("changed") do
  @options_list.each do |child|
    @options_list.remove child
  end
  @options_list.pack_start_defaults(Gtk::Label.new("hello"))
  # does only remove the label, but no new added
end

#~~~~~~~

# this works, the hello label is there
@options_list = Gtk::VBox.new(false,5)
@options_list.pack_start_defaults(Gtk::Label.new("hello"))
@options_list.each do |child|
  @options_list.remove child
end
@options_list.pack_start_defaults(Gtk::Label.new("hello"))
Igor P. (Guest)
on 2009-04-22 04:09
Kevin Redon wrote:
> i'm trying to remove and add a widget to a container when an event
> occur. removing and adding works outside the event, but only removing
> works within

It looks that your {{ @options_list }} variable is defined in different
scope(s). I should also point out that block scope is handled
differently in ruby 1.8 and 1.9, which may complicate your case even
further. From how you presented your program snippet and particularly
your {{ @options_list }} variable it is not clear whether it is an {{
instance variable }} or a {{ class instance variable }}. Instance
variables can only be used from within instance methods.

To test my claim that you have a scope problem change your "instance"
variable from {{ @options_list }} to global {{ $options_list }}. The
program should then work fine; i.e.: here is no "gtk container
add/remove malfunction"!

To correctly fix your program, and at the same time avoid any
differences between ruby 1.8 and 1.9, implement attribute accessor
methods and access your instance variables only through their respective
objects using the accessor methods (hint: attr_accessor).

Good luck,
Igor
Kevin R. (Guest)
on 2009-04-22 10:02
Igor P. wrote:
>
> To test my claim that you have a scope problem change your "instance"
> variable from {{ @options_list }} to global {{ $options_list }}. The
> program should then work fine; i.e.: here is no "gtk container
> add/remove malfunction"!
>

@options_list is a class instance variable defined in no class (so in
the class Object by default)
@options_list = Gtk::VBox.new(false,5)

I access @options_list within the same class Object (by default).
Yes it is not an malfunction, but something I don't understand.
The curiosity is that remove the child widget within the container works
in the signal block, so it should not be a scope problem. Only adding
element (even if not removing any) does not work.

replacing @options_list with $options_list does not solve the problem,
the result is the same.

> To correctly fix your program, and at the same time avoid any
> differences between ruby 1.8 and 1.9, implement attribute accessor
> methods and access your instance variables only through their respective
> objects using the accessor methods (hint: attr_accessor).
>
> Good luck,
> Igor

I'm accessing @options_list within the same object, I'm not sure I
should create an accessor for it, as I want to keep it private and use
it only within this Object class instance. Or should I ?

Thanks,
Kevin REDON
Kevin R. (Guest)
on 2009-04-22 14:58
the added elements are there, but not visible
the solution was simply : @options_list.show_all
This topic is locked and can not be replied to.