Skip to content

Hello World Example

drfeelngood edited this page Apr 10, 2012 · 2 revisions

This example shows how to create a simple GtkApp application.

First, install the gtk_app RubyGem.

$ gem install gtk_app

Next, create a fancy Gtk user interface using GtkBuilder as the file format.

hello_world.ui

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window">
    <property name="visible">True</property>
    <property name="border_width">12</property>
    <property name="title" translatable="yes">GtkApp Hello World Example</property>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <property name="spacing">6</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">&lt;b&gt;Hello World!&lt;/b&gt;</property>
            <property name="use_markup">True</property>
          </object>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkHButtonBox" id="hbuttonbox1">
            <property name="visible">True</property>
            <property name="layout_style">end</property>
            <child>
              <object class="GtkButton" id="btnQuit">
                <property name="label">gtk-quit</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_stock">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Now its time to create the application file. Here, we just make a simple module with a controller.

hello_world.rb

require 'rubygems'
require 'gtk_app'

module HelloWorld
  class Controller < GtkApp::Controller
    on :btnQuit, 'clicked' do |widget|
      GtkApp.quit
    end
  end
end

HelloWorld::Controller.new do
  @view = GtkApp::View.new(self, './hello_world.ui')
end

GtkApp.run

It's time to party!

$ ruby hello_world.rb
Clone this wiki locally