[ruby-gtk2] problem changing menu in Gtk::OptionMenu

D

David Espada

Hi all. I need (again) help with ruby-gtk2.

I have problems with this little code fragment:

---------------------------------------------
#!/usr/bin/ruby

require 'glib2'
require 'gtk2'

class BotonOpciones < Gtk::OptionMenu
def initialize(valores=nil, inicial=nil)
super()

add_options(valores, inicial) if valores
end

def add_options(valores, inicial=nil)
@valores = valores
@valor = inicial || valores[0]

# Remove last menu. Is this necesary?
if @menu
remove_menu
@menu.destroy
end

@items = []
@menu = Gtk::Menu.new
grupo = nil
@valores.each do |v|
item = Gtk::RadioMenuItem.new(grupo, v)
@items << item
grupo = item.group
@menu.append(item)
item.signal_connect('activate') do
@valor = v if @valor != v
end
end
set_menu(@menu)
put_value(inicial) if inicial
end

def put_value(valor)
@valores.each_with_index do |val, i|
if valor == val then
@items.active = true
history = i
return
end
end
end

def get_value
@valor
end
end

Gtk::init

ventana = Gtk::Window.new
ventana.set_title("Problema con OptionMenu")
ventana.signal_connect('delete_event') {exit}


vbox = Gtk::VBox::new(true, 10)

boton1 = BotonOpciones.new(['hola', 'hello'], 'hola')
boton2 = BotonOpciones.new(['adiós', 'bye'], 'bye')

boton1.signal_connect('changed') do
boton2.add_options(['otro', 'other'], 'otro')
end

vbox.pack_start(boton1, true, true, 0)
vbox.pack_start(boton2, true, true, 0)

ventana.add(vbox)

ventana.show_all

Gtk.main
-------------------------------------------

When I select value of boton1, boton2 doesn't change menu, but
instead I get an empty list.

I don't know if I am doing something wrong or this is a bug.

My system is a Debian Sid with libgtk2 packages from experimental
(version 2.4) and ruby-gnome2 from CVS.

Thanks.

David
 
M

Masao Mutoh

Hi,

Hi all. I need (again) help with ruby-gtk2.

I have problems with this little code fragment:
When I select value of boton1, boton2 doesn't change menu, but
instead I get an empty list.
I don't know if I am doing something wrong or this is a bug.

A new widget which you create in BotonOpciones#add_options doesn't
call Gtk::Widget#show or #show_all.
Try to call @menu.show_all after calling BotonOpciones#set_menu().

Everytime you need to call Gtk::Widget#show(#show_all) when you
want to show your Widgets.
 
D

David Espada

El miércoles 28 de abril, Masao Mutoh escribió:
Everytime you need to call Gtk::Widget#show(#show_all) when you
want to show your Widgets.

An obvious error 0:)

Thanks for all.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top