/GTK3 - ToggleButton

5211 단어 GTK3gtkRuby
gem install gtk3

ToggleButton


구글 버튼은 통상적인 GTK 버튼과 비슷하지만, 클릭 후 다시 클릭할 때까지 활성 상태를 유지한다.단추의 상태가 변할 때'toggled'신호가 발생합니다.

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
  • Python version
  • 좋은 웹페이지 즐겨찾기