Forms as Components

Forms are themselves components, which means you can place a Form inside another Form. This lets you build your UI in a modular way, breaking your interface into smaller, reusable pieces and combining them together.

Adding a Form to Another Form in Design view

To add a Form to another Form in the designer, click the Form’s name in the App Browser and drag it into the Design view of the Form you want to add it to.

A Blank Panel Form in the Design View with a Label and an Image in.

Form2 in the Design View

Dragging and dropping a Form from the Sidebar into another Form in the Designer

Dropping Form2 from the App Browser into Form1 in the Designer

You can also designate a Form as a Custom Component if you want it to appear in the Toolbox.

Adding a Form to Another Form in Code

As well as dragging and dropping Forms onto other Forms, you can also combine them in code using add_component(). In the section above, we added an instance of Form2 into the self.card_1 component of Form1 using drag-and-drop. Here’s how you would do that in code:

from anvil import *

# Import Form2 so we can create instances of it
from .Form2 import Form2

class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Add Form2 to self.card_1
    self.card_1.add_component(Form2())

Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.