Manually creating and adding components

What about for programmatically generated components?
I don’t know the name of them, so I can’t get hold of them . .

And when I create them I don’t know how to name them.

You can make up a property for any component, for example :

mylabel = Label()
mylabel.wibble="wobble"

If you need to refer to them you can loop through the components on the form, or you could maintain an array of components, eg :

self.mylabelarray=[]
mylabel = Label()
mylabel.wibble="wobble"
self.mylabelarray.append(mylabel)

Or you could keep a dictionary of them, eg :

self.mylabeldict={}
mylabel = Label()
self.mylabeldict['wobble']=mylabel

and refer to them like this :

self.mylabeldict['wobble'].text="hi"`
2 Likes

OK, I forgot I can name at creation like that . .

Waht about setting to a global variable?

Why doesn’t this work (If link1 clicked & then link2)?

class NavForm (NavFormTemplate):
  
  SplitFormComp = None
  RepFormComp = None
  
  def __init__(self, **properties):
    # You must call self.init_components() before doing anything else in this function
    self.init_components(**properties)
    self.content_panel.add_component(StoryForm())

  def link_1_click (self, **event_args):
    #self.RepFormComp.remove_from_parent()
    self.SplitFormComp = self.content_panel.add_component(SplitForm())
          
  def link_2_click (self, **event_args):
    
    self.SplitFormComp.remove_from_parent()
    self.RepFormComp = self.content_panel.add_component(RepForm())

(I have got around all this through searching thru get components and naming at creation . . but I would have preferred something like the above).

I’m not quite sure what that code is trying to do?
add_component() is not usually stored in a result variable - not even sure it returns anything.

Switch between 2 forms from a nav sidebar form.
It’s only part way there because you have to click the first link first.

I’m basically trying to build a nav side menu that:

  1. Creates the menu item page
  2. BUT leaves a CONSTANT form at the top.

That means I can’t use .clear() like in the Anvil demos.
I actually have to know the names to delete the page we’re leaving whilst NOT deleting the constant form at the top of the nav form.

OK, you’re saying I should just do:

self.SplitFormComp = SplitForm()

But in that case how do we position it in content_panel?
By setting its parent property?

Ah, I see.

The best way to do that is to have a column panel as your “content area”. That way you can clear just that and use “add_component()” into that content panel.

Unless I’m still not getting something?

So, on clicking a menu link, you’d do :

myform = MyContentForm()
self.column_panel_contentpage.clear()
self.column_panel_contentpage.add_component(myform)

where “column_panel_contentpage” is a column panel you’ve pre-added in the designer to your “Main” form that also contains the header form.

1 Like

Thnx.
That may be the solution . . as long as I can load a form into a column component (not just a content_panel whatever that is)
(BTW my fancy searching method does work . . but I like yours much better of course)

Here’s a quick example I just knocked up :

https://anvil.works/ide#clone:J42DGQAFZRAHUJAJ=3P4RCF7U2TNV67FM7BEOFUBI

Shows the principle I use in my apps.

Thanks, I just got it working too with your advice.

My basic problem was I thought the content_panel was some unique panel that could take forms . .
And I couldn’t figure out how to get an additional one because it came with the template.

1 Like

Moving next Q to a new topic . .

(Split and renamed by @david.wylie)