Hi, I just started learning the crystal language and it’s not clear to me when to use self
(maybe I missed something in docs).
I wrote a simple program in which I added inheritance from GTK::Button
in the descendant constructor I would like to set the label
value. According to the documentation it is a function - button docs. So why do I need to add self.label="text"
instead of use simply label="text"
in this case?
Full source code:
require "gtk4"
class MyButton < Gtk::Button
def initialize
super()
self.label = "My button title"
#label = "My button title" - not working
end
end
app = Gtk::Application.new("hello.example.com", Gio::ApplicationFlags::None)
count = 0
app.activate_signal.connect do
window = Gtk::ApplicationWindow.new(app)
window.title = "Hello World!"
window.set_default_size(200, 200)
hbox = Gtk::Box.new(Gtk::Orientation::Horizontal, 10)
hbox.spacing = 1
button = Gtk::Button.new_with_label("Hello!!")
button.clicked_signal.connect do
count += 1
button.label = "You clicked #{count} times!"
end
hbox.append(button)
mybutton = MyButton.new()
hbox.append(mybutton)
window.child = hbox
window.present
end
exit(app.run)
When I change self.label
to label
it is not working.