DEV Community

kojix2
kojix2

Posted on

Ruby/GTK3 - RaddioButton

gem install gtk3

RadioButton

Radio button allows the user to choose only one option.

raddio button

require 'gtk3'

class RadioButtonWindow < Gtk::Window
  def initialize
    super
    set_title 'RadioButton Demo'
    set_border_width 10

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

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

    button2 = Gtk::RadioButton.new(member: button1, label: 'Button 2')
    button2.signal_connect('toggled') { |b| on_button_toggled b, 2 }
    hbox.pack_start(button2)

    button3 = Gtk::RadioButton.new(member: button1, label: 'B_utton 3',
                                   use_underline: true)
    button3.signal_connect('toggled') { |b| on_button_toggled b, 3 }
    hbox.pack_start(button3)
  end

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

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

output

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

The options member and use_underline are defined here.

Top comments (0)