0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby/GTK3 - ToggleButton

Last updated at Posted at 2020-02-25

gem install gtk3

ToggleButton

トグルボタンは通常のGTKのボタンとよく似ているが、クリックすると再びクリックされるまでアクティブなまま、押されたままになる。ボタンの状態が変化すると「toggled」シグナルが発生する。

image.png

Button 2 was turned on
Button 1 was turned on
Button 2 was turned off
Button 1 was turned off

require 'gtk3'

class ToggleButtonWindow < Gtk::Window
  def initialize
    super
    self.title = "ToggleButton Demo"
    self.border_width = 10

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

    button = Gtk::ToggleButton.new(label: 'Button 1')
    button.signal_connect('toggled') { |b| on_button_toggled(b, 1) }
    hbox.pack_start(button)

    button = Gtk::ToggleButton.new(label: 'Button 2')
    button.signal_connect('toggled') { |b| on_button_toggled(b, 2) }
    button.set_active true
    hbox.pack_start(button)
  end

  def on_button_toggled(button, name)
    state = button.active? ? 'on' : 'off'
    puts "Button #{name} was turned #{state}"
  end
end

win = ToggleButtonWindow.new
win.signal_connect('destroy') { Gtk.main_quit }
win.show_all

Gtk.main
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?