BAHASA PEMROGRAMAN RUBY DI LINUX MENGGUNAKAN RUBY-GTk
Oleh:Slamet Nurhadi
DEPARTEMEN TEKNIK INFORMATIKA UNIVERSITAS NASIONAL DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
Menginstall Ruby dengan menggunakan kode dibawah ini ketik di console $> sudo apt-get install ruby-full libruby libruby-extras ruby1.8-dev
Untuk menginstall paket-paket Ruby-GNOME2 di Ubuntu Linux cukup dengan mengetikan perintah: $> sudo apt-get install ruby-gnome2
#Teknik Informatika #Universitas Nasional require 'gtk2'
class RubyApp < Gtk::Window def initialize super set_title "Simple menu" signal_connect "destroy" do Gtk.main_quit end init_ui set_default_size 250, 200 set_window_position Gtk::Window::POS_CENTER DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
show_all end def init_ui modify_bg Gtk::STATE_NORMAL, Gdk::Color.new(6400, 6400, 6440) mb = Gtk::MenuBar.new #membuat menubar file filemenu = Gtk::Menu.new filem = Gtk::MenuItem.new "File" filem.set_submenu filemenu mb.append filem #membuat menubar edit editmenu = Gtk::Menu.new editm = Gtk::MenuItem.new "Edit" editm.set_submenu editmenu mb.append editm # membuat sub menu save, import, exit dan undo imenu =Gtk::Menu.new importm =Gtk::MenuItem.new "import" importm.set_submenu imenu
inews = Gtk::MenuItem.new "Import news feed..."
ibookmarks = Gtk::MenuItem.new "Import bookmarks..." imail = Gtk::MenuItem.new "Import mail..." imenu.append inews imenu.append ibookmarks DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
imenu.append imail save =Gtk::MenuItem.new "save" save.signal_connect "activate" do Gtk.main_save end exit = Gtk::MenuItem.new "Exit" exit.signal_connect "activate" do Gtk.main_quit end undo =Gtk::MenuItem.new "undo" #membuat posisi dimana submenu akan ditaruh pada suatu menubar editmenu.append undo filemenu.append save filemenu.append importm filemenu.append exit vbox = Gtk::VBox.new false, 2 vbox.pack_start mb, false, false, 0 add vbox end end Gtk.init window = RubyApp.new Gtk.main
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
hasilnya seperti ini:
membuat menu dan toolbar #Teknik Informatika #Universitas Nasional require 'gtk2'
class RubyApp < Gtk::Window def initialize super set_title "Toolbar" signal_connect "destroy" do Gtk.main_quit end init_ui
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
set_default_size 300, 200 set_window_position Gtk::Window::POS_CENTER show_all end def init_ui
vbox = Gtk::VBox.new false, 5 hbox = Gtk::HBox.new true, 3 valign = Gtk::Alignment.new 0, 1, 0, 0 vbox.pack_start valign ok = Gtk::Button.new "OK" ok.set_size_request 70, 30 close = Gtk::Button.new "Close" hbox.add ok hbox.add close
halign = Gtk::Alignment.new 1, 0, 0, 0
halign.add hbox mb = Gtk::MenuBar.new #membuat menubar file
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
filemenu = Gtk::Menu.new filem = Gtk::MenuItem.new "File" filem.set_submenu filemenu mb.append filem #membuat menubar edit
editmenu = Gtk::Menu.new editm = Gtk::MenuItem.new "Edit" editm.set_submenu editmenu mb.append editm # membuat sub menu save, import, exit dan undo imenu =Gtk::Menu.new importm =Gtk::MenuItem.new "import" importm.set_submenu imenu
inews = Gtk::MenuItem.new "Import news feed..."
ibookmarks = Gtk::MenuItem.new "Import bookmarks..." imail = Gtk::MenuItem.new "Import mail..." imenu.append inews imenu.append ibookmarks imenu.append imail save =Gtk::MenuItem.new "save" exit = Gtk::MenuItem.new "Exit" exit.signal_connect "activate" do DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
Gtk.main_quit end undo =Gtk::MenuItem.new "undo" #membuat posisi dimana submenu akan ditaruh pada suatu menubar editmenu.append undo filemenu.append save filemenu.append importm filemenu.append exit
toolbar = Gtk::Toolbar.new toolbar.set_toolbar_style Gtk::Toolbar::Style::ICONS newtb = Gtk::ToolButton.new Gtk::Stock::NEW opentb = Gtk::ToolButton.new Gtk::Stock::OPEN savetb = Gtk::ToolButton.new Gtk::Stock::SAVE sep = Gtk::SeparatorToolItem.new cuttb = Gtk::ToolButton.new Gtk::Stock::CUT copytb = Gtk::ToolButton.new Gtk::Stock::COPY quittb = Gtk::ToolButton.new Gtk::Stock::QUIT toolbar.insert 0, newtb toolbar.insert 1, opentb toolbar.insert 2, savetb toolbar.insert 3, sep toolbar.insert 4, cuttb toolbar.insert 5, copytb DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
toolbar.insert 6, quittb quittb.signal_connect "clicked" do Gtk.main_quit end
#mengaktifkan toolbar dan menubar serta seting posisi vbox = Gtk::VBox.new false, 2 vbox.pack_start mb, false, false, 0 vbox.pack_start toolbar, false, false, 0 vbox.pack_start halign, false, false, 3 add (vbox) end end Gtk.init window = RubyApp.new Gtk.main
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
hasilnya seperti ini:
------Ticket----$KCODE require "gtk2" class TicketWindow < Gtk::Window def initialize
super("Purchase Ticket") signal_connect("destroy") { Gtk.main_quit } dest_model = Gtk::ListStore.new(String, String)
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
dest_view = Gtk::TreeView.new(dest_model) dest_column = Gtk::TreeViewColumn.new("Destination", Gtk::CellRendererText.new, :text => 0) dest_view.append_column(dest_column) country_column = Gtk::TreeViewColumn.new("Country", Gtk::CellRendererText.new, :text => 1) dest_view.append_column(country_column) dest_view.selection.set_mode(Gtk::SELECTION_SINGLE) [["Cairo", "Egypt"], ["New York", "USA"], ["Tokyo", "Japan"],["Jakarta", "Indonesia"]].each do | destination, country| iter = dest_model.append iter[0] = destination iter[1] = country end dest_view.selection.signal_connect("changed") do @city = dest_view.selection.selected[0] end @round_trip = Gtk::CheckButton.new("Round Trip") purchase = Gtk::Button.new("Purchase") purchase.signal_connect("clicked") { cmd_purchase } @result = Gtk::Label.new @coach = Gtk::RadioButton.new("Coach class") @business = Gtk::RadioButton.new(@coach, "Business class") DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
@first = Gtk::RadioButton.new(@coach, "First class") flight_box = Gtk::VBox.new flight_box.add(dest_view).add(@round_trip) seat_box = Gtk::VBox.new seat_box.add(@coach).add(@business).add(@first) top_box = Gtk::HBox.new top_box.add(flight_box).add(seat_box) main_box = Gtk::VBox.new main_box.add(top_box).add(purchase).add(@result)
set_window_position Gtk::Window::POS_CENTER add(main_box) show_all end def cmd_purchase text = @city if @first.active? text += ": first class" elsif @business.active? text += ": business class" elsif @coach.active? text += ": coach" end text += ", round trip " if @round_trip.active? @result.text = text DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
end end Gtk.init TicketWindow.new Gtk.main
hasilnya seperti ini:
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
----Window---require 'gtk2'
class RubyApp < Gtk::Window def initialize super set_title "Windows" signal_connect "destroy" do Gtk.main_quit end init_ui set_default_size 300, 250 set_window_position Gtk::Window::POS_CENTER show_all end def init_ui set_border_width 15 table = Gtk::Table.new 8, 4, false table.set_column_spacings 3 title = Gtk::Label.new "Windows"
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
halign = Gtk::Alignment.new 0, 0, 0, 0 halign.add title table.attach(halign, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL, 0, 0) frame = Gtk::Frame.new table.attach(frame, 0, 2, 1, 3, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND, 1, 1) activate = Gtk::Button.new "Activate" activate.set_size_request 50, 30 table.attach(activate, 3, 4, 1, 2, Gtk::FILL, Gtk::SHRINK, 1, 1) valign = Gtk::Alignment.new 0, 0, 0, 0 close = Gtk::Button.new "Close" close.set_size_request 70, 30 valign.add close table.set_row_spacing 1, 3 table.attach(valign, 3, 4, 2, 3, Gtk::FILL, Gtk::FILL | Gtk::EXPAND, 1, 1) halign2 = Gtk::Alignment.new 0, 1, 0, 0 help = Gtk::Button.new "Help" help.set_size_request 70, 30 halign2.add help table.set_row_spacing 3, 6 table.attach(halign2, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL, 0, 0) DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
ok = Gtk::Button.new "OK" ok.set_size_request 70, 30 table.attach(ok, 3, 4, 4, 5, Gtk::FILL, Gtk::FILL, 0, 0) add table end end Gtk.init window = RubyApp.new Gtk.main hasilnya seperti ini:
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
CATATAN PENTING: Instalasi Ruby dan Ruby/GTK Mencoba Bahasa Pemrograman Ruby di Linux menggunakan Ubuntu. Ruby/GTK in. Jangan lupa untuk menginstall bahasa pemrograman Ruby terlebih dahulu: $> sudo apt-get install ruby-full libruby libruby-extras ruby1.8-dev
Untuk menginstall paket-paket Ruby-GNOME2 di Ubuntu Linux cukup dengan mengetikan perintah: $> sudo apt-get install ruby-gnome2
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL