How to access the name property

hi

i have several components inside a form but i would like to only “register” a handful of the on the form. with register i mean that i would like to store a reference or some property of these components inside an array or dict.

the main property which i am interested in is the Name property.

is there a way to access this Name property?

i have tried with a button to alert self.name when the button is clicked. this will give me an exception, but i was expecting to get the button name inside the alert.

thanks!

As I understand it, very few Python objects have a “Name” property. (And if they did, it would likely be spelled “name”).

There’s a good reason for this: the object may be referred to from many different variables, each with its own name. Which “name” actually belongs to the object?

None of them. The name is a feature of the variable, not the object it happens to point to at the moment.

Edit: You can easily collect references to a form’s widgets. E.g., in the form’s __init__ function:

self.registered_widgets = [ self.radio_button1, self.text_box1, ...]

Can you provide a bit more context as to what you’re trying to achieve here?

As @p.colbert has said, this isn’t possible as far as I know. If you provide some more details we can offer alternatives.

For example, could simply using the text property of your button work?

Of course, here is what I am trying to accomplish.

When the form loads I will populate a object/dict with tag names from a PLC. A tag has the following name structure, “connection_device_system_unit”. E.g. “plc1_as01_as01_gt31”. This “tag” has for example the value of 25.

This app will contain multiple forms but all forms will have 3 properties which are for example self.connection (plc1), self.device(as01), self.system(as01).

I would then like to, for example, add a label to the form, name it “gt31” or enter gt31 as a property value of the label. Then I would like the label to use the form properies, self.connection, self.device, self.system AND the name or other property of the label to “build up” the tag name. Eg. Plc1_as01_as01_gt31. After the tag name has been build up it will look at the object/dict. If there’s a matching tag name, the label will display the value 25.

The values inside the forms object/dict will be updated from the PLC using a timer.
I was hoping what I could use Databindings of eg the label so the labels value gets updated then the forms object/dict gets updated.

I understand. But is there a way to register the widgets automatically? I would not like to “hard code” each widget.

And as I understand it is not possible to iterate over self (the form).

How would you distinguish between the ones you want, and the ones you don’t? However you make the choice, Anvil can’t make it for you, so it can’t be “automatic”. Literally.

Actually, since each Form is a container, it is. See Anvil Docs | Containers . This won’t get you the names of widgets. It gets you references to the actual widget objects.

That’s why I would like to use the name properly to filter out those widgets that I want :slight_smile:

And what are radio_button1 and text_box1 in the following?

These are the names you were looking for.

You also need a name (registered_widgets) for the variable containing the registry.

None of these names is going to appear by magic. They need to be named, and used, explicitly, in a manner that Python understands, using standard Python syntax and semantics. The above example does that.

Thanks, for bearing with me! And Sorry if I described my scenario poorly.

Let’s say that the label should register itself when the form loads. I have been looking at the labels Show event. How can I access the label reference inside the show event function without typing self.Label1 for example. I would like to use a generic keyword.

I’m still trying to figure out things with python since I am from the JavaScript world :slight_smile:

If I would have had a onShow event in JavaScript on a label, I have access to the label object using “this”. I could have then passed “this” (reference to the label) to another function or add to an global object (xxx.push(this)).

See Finding out what is in your event-handler's event_args

By default, the ‘sender’ is the object that invoked the handler. Thus, you could have any number of labels use the same handler function. The handler would get a reference to the invoking object (not its name!) as the value of parameter event_args['sender'].

Of course, setting the handler for each label is anything but automatic. You either set it manually, in the IDE, or you write (more) code to do it. In what order the labels will be “shown”, may be hard to guess, and might not be stable, depending on what edits you make along the way.

So, be aware, the “show-handler” approach is likely to be much less “automatic”, and more code, than simply

self.widgets_by_position = [ self.lbl_main, self.lbl_c1, ... ]
self.widgets_by_name = { "main": self.lbl_main, "column1": self.lbl_c1, ... }
1 Like

Thanks! Is it possible to pm you directly @p.colbert ?
I might be completely wrong with my strategy/structure regarding this app.

Sorry, I was interrupted by a tropical storm.

Yes, in this case, I’ll accept a private message. I appreciate your asking, first.