Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control.parent property #2906

Merged
merged 5 commits into from
Apr 3, 2024
Merged

Control.parent property #2906

merged 5 commits into from
Apr 3, 2024

Conversation

ndonkoHenri
Copy link
Collaborator

@ndonkoHenri ndonkoHenri commented Mar 18, 2024

Closes #952

  • Issue TBF: The tiles created on_click of the first tile have no parent (None) although they all live in thesame ListView.

Test Code:

import flet as ft


def main(page: ft.Page):
    def handle_deletion_from_parent(e: ft.ControlEvent):
        tile = e.control.parent
        lv = tile.parent
        lv.controls.remove(tile)
        page.update()

    def handle_addition_to_parent(e: ft.ControlEvent):
        my_tile = ft.ListTile(
            trailing=ft.IconButton(
                ft.icons.DELETE,
                on_click=handle_deletion_from_parent,
                tooltip="Delete tile from parent",
            ),
            title=ft.Text(
                "I am a newly created tile. Click me too to add a new tile to my parent"
            ),
            on_click=handle_addition_to_parent,
        )
        lv = e.control.parent
        lv.controls.append(my_tile)
        page.update()

    page.add(
        ft.ListView(
            controls=[
                ft.ListTile(
                    trailing=ft.IconButton(
                        ft.icons.DELETE,
                        on_click=handle_deletion_from_parent,
                        tooltip="Delete tile from parent",
                    ),
                    title=ft.Text("Click me to add a new tile to my parent"),
                    on_click=handle_addition_to_parent,
                )
                for i in range(2)
            ]
        )
    )


ft.app(target=main)

@ndonkoHenri ndonkoHenri linked an issue Mar 18, 2024 that may be closed by this pull request
@@ -515,6 +516,8 @@ def _build_add_commands(self, indent=0, index=None, added_controls=None):
indent=indent + 2, index=index, added_controls=added_controls
)
commands.extend(childCmd)
# set the parent
control.parent = self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That function where it's set is recursive, so control.parent will be set to a top most control which update() is called for.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your suggestion?

@ndonkoHenri
Copy link
Collaborator Author

ndonkoHenri commented Mar 28, 2024

Issue TBF: The tiles created on_click of the first tile have no parent (None) although they all live in thesame ListView.

Resolved! Property works like a charm now - try the updated test code :)

@FeodorFitsner FeodorFitsner merged commit 3409085 into main Apr 3, 2024
1 of 2 checks passed
@ndonkoHenri ndonkoHenri deleted the control-parent branch April 3, 2024 06:24
zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* initial commit

* remove parent when unmounting

* update parent typing

* set parent when adding/replacing

---------

Co-authored-by: Feodor Fitsner <feodor@appveyor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Control.parent property
2 participants