How to map Glade/Gtk widgets to instance variable without repeating yourself
Inserito da assente il 25 Settembre, 2006 - 21:44
Gnome | Ruby | Tecnologia / software libero
Glade is nice tool: it creates the xml of the inteface you see, but when you have to use this gui you have to map every widget created with an instance variable.
This is easy to mantain when you have few widgets, but when you have a lot textbox / entry you will have to map a lot of stuff.
Here's a Ruby version of a runtime code generation using the eval method
Download first the gui.glade
Yo will be able to access to any entry widget with @entry and set a common signal to all of them without doing it for each widget.
This is easy to mantain when you have few widgets, but when you have a lot textbox / entry you will have to map a lot of stuff.
Here's a Ruby version of a runtime code generation using the eval method
Download first the gui.glade
# gui.rb
require 'libglade2'
class Gui
def initialize
@glade = GladeXML.new("gui.glade") {|handler| method(handler)}
@glade.widget_names.each do |name|
begin
instance_variable_set("@#{name}".intern, @glade[name])
if @glade[name].class == Gtk::Entry
# automatic version
eval <<-END
@#{name}.signal_connect('changed') do
@window1.title= @#{name}.text
end
@#{name}.text= "My name is @#{name}"
END
end
rescue
end
end
# hand version
@window1.signal_connect('destroy'){
exit
}
@window1.show_all
end
end
Gtk.init
Gui.new
Gtk.main
ConclusionYo will be able to access to any entry widget with @entry
» letto 3231 volte


il link non va
il link non va... :( qualcuno ha una copia di gui.glade? grazie 1000. ciao da Marco.