-
Notifications
You must be signed in to change notification settings - Fork 0
7. Button Widgets
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 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 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
Radio and check button are inherited from toggle button. Radio button works in a group and only one 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().