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 liberoThis 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



Commenti recenti
4 ore 20 min fa
2 settimane 2 giorni fa
2 settimane 4 giorni fa
2 settimane 5 giorni fa
2 settimane 5 giorni fa
2 settimane 6 giorni fa
3 settimane 9 ore fa
3 settimane 2 giorni fa
3 settimane 3 giorni fa
3 settimane 5 giorni fa