DEV Community

kojix2
kojix2

Posted on

Ruby/GTK3 - Button

gem install gtk3

Button

Alt Text

"Click me" button was clicked
"Open" button was clicked
Closing application

require 'gtk3'

class ButtonWindow < Gtk::Window
  def initialize
    super
    set_title 'Button Demo'
    set_border_width 10

    hbox = Gtk::Box.new(:horizontal, 6)
    add(hbox)

    button = Gtk::Button.new(label: 'Click Me')
    button.signal_connect('clicked') { on_click_me_clicked }
    hbox.pack_start(button)

    button = Gtk::Button.new(mnemonic: '_Open')
    button.signal_connect('clicked') { on_open_clicked }
    hbox.pack_start(button)

    button = Gtk::Button.new(mnemonic: '_Close')
    button.signal_connect('clicked') { on_close_clicked }
    hbox.pack_start(button)
  end

  def on_click_me_clicked
    puts '"Click me" button was clicked'
  end

  def on_open_clicked
    puts '"Open" button was clicked'
  end

  def on_close_clicked
    puts 'Closing application'
    Gtk.main_quit
  end
end

win = ButtonWindow.new
win.signal_connect('destroy') { Gtk.main_quit }
win.show_all
Gtk.main
Enter fullscreen mode Exit fullscreen mode

Python version

Top comments (0)