Skip to content
Ebru Akagündüz edited this page Feb 15, 2015 · 4 revisions

2.1. Simple Example

We create an empty window.

#!/usr/bin/ruby
require "gtk3"
win = Gtk::Window.new
win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main

#!/usr/bin/ruby this line indicates, this is a ruby file and must be run using specifed path which is /usr/bin/ruby. We do not have to specify the first line, it only provides to run executable files with dot slash without specify command name. If we do not add the line, we should run the file with ruby file_name.rb

win = Gtk::Window.new creates an empty window.

win.signal_connect("destroy"){Gtk.main_quit} ensures an safety way to close window. If we do not add the line the window still can be closed clicking icon x however program's process will be alive.

win.show_all displays window. Gtk.main runs the program untill Gtk.main_quit is called

2.2 Extended Example

We add a button on window.

require "gtk3"
win = Gtk::Window.new
button = Gtk::Button.new(:label => "Hello World")
win.add(button)
# print hello world to terminal
button.signal_connect("clicked") {puts "Hello World"}
win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main

button = Gtk::Button.new(:label => "Hello World") define new button. win.add(button) add the button object to window. Here window view is not very good. We should have used Fixed object to display better view but this is just a simple example. Let's see advanced examples on later topics.

Clone this wiki locally