Gtk container add/remove malfunction


#1

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


#2

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


#3

the added elements are there, but not visible
the solution was simply : @options_list.show_all


#4

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