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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay