DEV Community

Cover image for Ruby/GTK3 - Switch
kojix2
kojix2

Posted on

Ruby/GTK3 - Switch

gem install gtk3

Switch

A Gtk.Switch is a widget that has two states: on or off.
You can use the notify::active signal.

Lots of switches

require 'gtk3'

class SwitcherWindow < Gtk::Window
  def initialize
    super
    set_title 'Switch Demo'
    set_border_width 10

    grid = Gtk::Grid.new
    grid.set_column_spacing 6
    grid.set_row_spacing 6
    add grid

    4.times do |i|
      2.times do |j|
        switch = Gtk::Switch.new
        switch.signal_connect('notify::active') { |s| on_switch_activated s }
        switch.set_active [true, false].sample
        grid.attach switch, i, j, 1, 1
      end
    end
  end

  def on_switch_activated(switch)
    state = switch.active? ? 'on' : 'off'
    puts "Switch was turned #{state}"
  end
end

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

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more