-
Notifications
You must be signed in to change notification settings - Fork 0
3. Basics
Everything is an object in Ruby. When we create button, label, window etc., the widgets have properties. We can set properties using special functions or with keywords. Following two examples shown below that can set properties during declaration time or later.
button = Gtk::Button.new(:label => "About") # set button text with keyword
##########################################################################
button = Gtk::Button.new
button.set_label("About") # set button text using function
Gtk+ uses event-driven programming model. In Gtk.main loop program waits for defined events, when user triggered the event, Gtk+ starts doing something. clicked, destroy, key-press-event, button-press-event, switch-page etc. theese are signal keywords in Gtk+. To trigger signals, we use signal_connect().
button.signal_connect("clicked") {puts "Hello World"}
When the button got click operation, puts "Hello World" run. Also we can call any function in curly braces.
# we can call more than one function seperating with semicolon.
button.signal_connect("clicked") {on_click_function(); another_function();}
Instead of curly braces we can use do ... end blocks.
button.signal_connect("clicked") do
puts "Hello World"
function1()
function2()
end
If we want to grab returned variables from signal_connect():
# grab values from the signal
iconview.signal_connect("motion-notify-event") do |widget, event|
# event variable used to get icon path
@iconview_path = iconview.get_path_at_pos(event.x, event.y)
end
motion-notify-event returns two values, I have grabbed and used them in the block.
Note: In do ... end block, variables will be local and you can't use them outside of the block.