Sunday, February 14, 2016

Building Ruby App using GTK3 with Glade3 on Linux Mint 17.2

I have created a simple ruby program using gtk3 with Glade3.

1. References

Installing GTK3 on Linux
Installing Glade3 on Linux
How to use Glade resource with Ruby Gtk3

2. Create a Glade file

I have created a simple Glade file. For this simple test, I just added a main window ('window1') and specified a signal handler ('quit') for destroy event of the main window.
GladeTest.glade

3. Write a Ruby code using the glade file

Here is my ruby program that load the glade file that I created above and use it for user interface.
(1) Create a Gtk Builder object ('Gtk::Builder.new')
(2) Attach ui elements by loading the glade file ('add_from_file')
(3) Connect all signals to their handlers ('connect_signals {...}')
(4) Define signal handler method ('quit')
(5) Get object id for main window ('get_object')
(6) Show all widgets of the main window ('show')
(7) Finally call main loop ('Gtk.main')

glade_test.rb

4. Run the ruby program

$ ruby glade_test.rb

5. Display

Here is the window that this Ruby program displays.

6. Conclusion

I found it extremely difficult to find good tutorials or sample codes that use GTK3, especially ones with Glade3. Luckily I found one small sample codes by Gregory Romé. It turned out to be quite simple to load a glade (xml) file and use it.

No comments:

Post a Comment