In this script there is a button click callback, and I would like try to modify the text of the label in the module when the button is clicked, but nothings happen.
clicked_callback = function (_)
label.label = _("button clicked")
end
this is the label:
local label = dt.new_widget("label")
label.label = _("my ") .. "label" -- This is an alternative way to the "{}" syntax to set a property
Okay, it works, this changes the button’s label… this is something, thank you, because I couldn’t get him to do this either.
Maybe I was misunderstood, there is a Label in the module (as in the example code), and I’d like to change it’s label (text) when the button is clicked.
local label = dt.new_widget("label")
label.label = _("my ") .. "label"
Ok, now I understand. The problem is that the label is defined after the button, so when the button is declared the label doesn’t exist. We can work around that using the mE.widgets table.
Here’s the ugly but effective way to do it
clicked_callback = function (_)
local mylabel = mE.widgets[5]
mylabel.label = _("button clicked")
end
This works fine for playing around, but if you are going to write code you are going to use then you are better off creating a widgets table and using named widgets:
This way when you want to change a specific label it’s easier to change widgets.middle_label rather than widgets[2]. It’s also much easier when you go back to that code a year later and you’re trying to figure out what you did.