Skip to content

Instantly share code, notes, and snippets.

@gpr
Last active November 4, 2019 19:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gpr/3512c3e66022249c833f to your computer and use it in GitHub Desktop.
Save gpr/3512c3e66022249c833f to your computer and use it in GitHub Desktop.
How to use Glade resource with Ruby Gtk3
#!/usr/bin/env ruby
require 'gtk3'
def not_yet_implemented(object)
puts "#{object.class.name} sent a signal!"
end
def on_main_window_destroy(object)
Gtk.main_quit()
end
main_window_res = 'main_window.xml'
builder = Gtk::Builder.new
builder.add_from_file(main_window_res)
# Attach signals handlers
builder.connect_signals do |handler|
begin
method(handler)
rescue
puts "#{handler} not yet implemented!"
method('not_yet_implemented')
end
end
main_window = builder.get_object('main_window')
main_window.show()
Gtk.main
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkWindow" id="main_window">
<property name="width_request">100</property>
<property name="height_request">40</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<signal name="destroy" handler="on_main_window_destroy" swapped="no"/>
<child>
<object class="GtkButton" id="button">
<property name="label">gtk-execute</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="on_button_clicked" swapped="no"/>
</object>
</child>
</object>
</interface>
@geomeiam
Copy link

geomeiam commented Nov 4, 2019

Thanks for doing this.Very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment