# Gtk container add/remove malfunction

**URL:** <https://www.ruby-forum.com/t/gtk-container-add-remove-malfunction/166106>\
**Category:** Ruby\
**Created:** [April 21, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/gtk-container-add-remove-malfunction/166106 "2009-04-21T22:33:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dotty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@dotty](https://www.ruby-forum.com/u/dotty)\
**Post date:** [April 21, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/gtk-container-add-remove-malfunction/166106/1 "2009-04-21T22:33:33Z")

</div>

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”))

---

<div class="post-metadata">

**Author:** ![dotty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@dotty](https://www.ruby-forum.com/u/dotty)\
**Post date:** [April 22, 2009, 2:09am UTC](https://www.ruby-forum.com/t/gtk-container-add-remove-malfunction/166106/2 "2009-04-22T02:09:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@dotty](https://www.ruby-forum.com/u/dotty)\
**Post date:** [April 22, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/gtk-container-add-remove-malfunction/166106/3 "2009-04-22T12:58:04Z")

</div>

the added elements are there, but not visible  
the solution was simply : @options\_list.show\_all

---

<div class="post-metadata">

**Author:** ![dotty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@dotty](https://www.ruby-forum.com/u/dotty)\
**Post date:** [April 22, 2009, 8:02am UTC](https://www.ruby-forum.com/t/gtk-container-add-remove-malfunction/166106/4 "2009-04-22T08:02:26Z")

</div>

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
