Skip to content
Ebru Akagündüz edited this page May 1, 2015 · 8 revisions

7.1 Button

Buttons are mostly used type of widget. You can find many signal types for them, in general used to realize something when button is triggered.

"clicked" signal is common used one, is used when button has been pressed and released.

#!/usr/bin/ruby
require "gtk3"

win = Gtk::Window.new
win.set_title("Button Demo")
win.set_border_width(10)

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

button = Gtk::Button.new(:label => "Click Me")
button.signal_connect("clicked") {puts "\"Click Me\" button was clicked"}
hbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

button = Gtk::Button.new(:stock_id => Gtk::Stock::OPEN)
button.signal_connect("clicked") {puts "\"Open\" button was clicked"}
hbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

button = Gtk::Button.new(:label => "_Close", :mnemonic => true)
button.signal_connect("clicked") {puts "Closing application"; Gtk.main_quit;}
hbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main 

7.2 Toggle Button

Toggle button is quite similar to button. With first click, it stays pressed and then second click provides to release it. You can supply to stay pressed from code using set_active(). It gets label only with string instead of :label symbol.

#!/usr/bin/ruby
require "gtk3"

def on_button_toggled(button, name)
  if button.active?
    state = "on"
  else
    state = "off"
  end
  puts "Button" + name + " was turned" + state
end

win = Gtk::Window.new
win.set_title("ToggleButton Demo")
win.set_border_width(10)

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

button = Gtk::ToggleButton.new("Button 1")
button.signal_connect("toggled") {|button| on_button_toggled(button, "1")}
hbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

button = Gtk::ToggleButton.new("B_utton 2", :use_underline => true)
button.set_active(true)
button.signal_connect("toggled") {|button| on_button_toggled(button, "2")}
hbox.pack_start(button, :expand => true, :fill => true, :padding => 0)

win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main 

7.3 Check Button

Check button has two different state like toggle button. You can get button state with active? method. The button seems like a label and have a little box near of it. We used the button many times here: github.com/COMU/rubygtk3/wiki/6.-Entry

7.4 Radio Button

Radio and check button are inherited from toggle button. Radio button works in a group and only one button can be selected at any time. If you want to force users for one choice, use radio button, does things otomatically for you.

At first run, one radio button will be selected. Afterward you can change using set_active().

In following example; button1= Gtk::RadioButton.new("Button 1") initialize first button. Gtk::RadioButton.new(button1, "B_utton 3", true) specify third button that belongs to same button group with button1 and sets :use_underline as true. So there are three different usages of initalizing a radio button:

  • Gtk::RadioButton.new("button-name")
  • Gtk::RadioButton.new(button, "button-name")
  • Gtk::RadioButton.new(button, "button-name", :use_underline)
    :use_underline is false as default.
#!/usr/bin/ruby
require "gtk3"

def on_button_toggled(button, name)
  if button.active?
    state = "on"
  else
    state = "off"
  end
  puts "Button " + name + " was turned " + state
end

win = Gtk::Window.new
win.set_title("RadioButton Demo")
win.set_border_width(10)

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

button1 = Gtk::RadioButton.new("Button 1")
button1.signal_connect("toggled") {|button| on_button_toggled(button, "1")}
hbox.pack_start(button1, :expand => false, :fill => false, :padding => 0)

button2 = Gtk::RadioButton.new(button1, "Button 2")
button2.signal_connect("toggled") {|button| on_button_toggled(button, "2")}
hbox.pack_start(button2, :expand => false, :fill => false, :padding => 0)

button3 = Gtk::RadioButton.new(button1, "B_utton 3", true)
button3.signal_connect("toggled") {|button| on_button_toggled(button, "3")}
hbox.pack_start(button3, :expand => false, :fill => false, :padding => 0)

win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main 

7.5 Link Button

Link button is similar to Gtk::Button, when clicked it, provides to access specified hyperlink. You can get the link with button.uri and set with button.set_uri("..").

#!/usr/bin/ruby
require "gtk3"

win = Gtk::Window.new
win.set_title("LinkButton Example")
win.set_border_width(10)

button = Gtk::LinkButton.new("http://www.gtk.org", "Visit GTK+ Homepage")
win.add(button)

win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main

7.6 Spin Button

Spin button provides a range to specfiy a value. It shows two different arrow to increase and decrease value, clicking one arrow at any time you can change the value. Rather than using Gtk::Entry, spin button supplies a defined format, entering value into it manually is available as well.

To get and set current value of the button, use spinbutton.value and spinbutton.set_value respectively. Spin button has showing different type of values like integer, float. You can adjust it.

Spin button accepts value, even if it is not valid. You can able to change this policy, if you want, avoid this property using Gtk::SpinButton::UpdatePolicy::IF_VALID

#!/usr/bin/ruby
require "gtk3"

def on_numeric_toggled(button, spinbutton)
  spinbutton.set_numeric(button.active?)
end

def on_ifvalid_toggled(button, spinbutton)
  if button.active?
    policy = Gtk::SpinButton::UpdatePolicy::IF_VALID
  else
    policy = Gtk::SpinButton::UpdatePolicy::ALWAYS
  end
  spinbutton.set_update_policy(policy)
end

win = Gtk::Window.new
win.set_title("SpinButton Example")
win.set_border_width(10)

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

adjustment = Gtk::Adjustment.new(0, 0, 100, 1, 10, 0)
spinbutton = Gtk::SpinButton.new
spinbutton.set_adjustment(adjustment)
hbox.pack_start(spinbutton, :expand => false, :fill => false, :padding => 0)

check_numeric = Gtk::CheckButton.new("Numeric")
check_numeric.signal_connect("toggled") {|button| on_numeric_toggled(button, spinbutton)}
hbox.pack_start(check_numeric, :expand => false, :fill => false, :padding => 0)

check_ifvalid = Gtk::CheckButton.new("If Valid")
check_ifvalid.signal_connect("toggled") {|button| on_ifvalid_toggled(button, spinbutton)}
hbox.pack_start(check_ifvalid, :expand => false, :fill => false, :padding => 0)

win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main 

7.7 Switch Button

Switch button has two state which are on and off. You can change the state by clicking unselected side or dragging it.

#!/usr/bin/ruby
require "gtk3"

def on_switch_activated(switch)
  if switch.active?
    state = "on"
  else
    state = "off"
  end
  puts "Switch was turned " + state
end

win = Gtk::Window.new
win.set_title("SwitchButton Example")
win.set_border_width(10)

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

switch = Gtk::Switch.new
switch.signal_connect("notify::active"){|switch| on_switch_activated(switch)}
switch.set_active(true)
hbox.pack_start(switch, :expand => true, :fill => true, :padding => 0)


win.signal_connect("destroy"){Gtk.main_quit}
win.show_all
Gtk.main