Why the next error is showing? AttributeError: 'ControlEvent' object has no attribute 'productRow'

75 views Asked by At

Im making a Flet app and im try to make a responsive row where the user can append a conteiner with especific information. The point is that in a way or another i always get the next error: AttributeError: 'ControlEvent' object has no attribute 'productRow'. The thing is this action is inside a method that sends the conteiner template to the row. Im doing it this way because i found a example that makes the same thing but with a checkbox instead of the conteiner. This is the example:

import flet as ft

class TodoApp(ft.UserControl):
    def build(self):
        self.new_task = ft.TextField(hint_text="Whats needs to be done?", expand=True)
        self.tasks = ft.Column()

        # application's root control (i.e. "view") containing all other controls
        return ft.Column(
            width=600,
            controls=[
                ft.Row(
                    controls=[
                        self.new_task,
                        ft.FloatingActionButton(icon=ft.icons.ADD, on_click=self.add_clicked),
                    ],
                ),
                self.tasks,
            ],
        )

# This especific part:

    def add_clicked(self, e):
        self.tasks.controls.append(ft.Checkbox(label=self.new_task.value))
        self.new_task.value = ""
        self.update()


def main(page: ft.Page):
    page.title = "ToDo App"
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    page.update()

    # create application instance
    todo = TodoApp()

    # add application's root control to the page
    page.add(todo)

ft.app(target=main)

And the next code is my App. The app its an POS software, right now im trying to make this class work because i want to make the data store part but i cant before doing this. Sorry for my bad english...


import flet as ft

class TopApp(ft.UserControl):

    def build(self):
        def close_addProduct(self):
            addProduct.open = False
            self.page.update()
            for i in range(3):
                NewProduct[i].value = ""

        def open_addProduct(self):
            self.page.dialog = addProduct
            addProduct.open = True
            self.page.update()

        def close_Search(self, nameProduct):
            self.text = nameProduct
            SearchProducts.close_view(self.text)

        NewProduct = [
            ft.TextField(label="Nombre"),
            ft.TextField(label="Precio"),
            ft.TextField(label="Imagen")
        ]

        addProduct = ft.AlertDialog(
            title=ft.Text("Agrega tus productos"),
            modal=True,
            content=ft.Column(
                NewProduct
            ),
            actions=[
                ft.TextButton(text="Cancelar", on_click=close_addProduct),
                ft.TextButton(text="Agregar", on_click=CenterApp.submittBttn, )
            ],
        )

        AddButton = ft.FloatingActionButton(
            icon=ft.icons.ADD, on_click=open_addProduct, bgcolor=ft.colors.LIGHT_BLUE_ACCENT_400
        )

        SearchProducts = ft.SearchBar(
            view_elevation=4,
            divider_color=ft.colors.LIGHT_BLUE_ACCENT_400,
            bar_hint_text="Buscador de productos",
            view_hint_text="Qué producto estas buscando...",
            width=900,
            on_change=None,
            on_submit=None,
            controls=[
                ft.ListTile(title=ft.Text(), on_click=close_Search)
            ],
        )
        return ft.Row(
            controls=[
                AddButton,
                SearchProducts
            ]
        )


class CenterApp(ft.UserControl):
    productRow = ft.ResponsiveRow(controls=[])

    def build(self):

        return ft.Column(
            width=600,
            controls=[self.productRow],  # Agrega el botón a los controles
        )

    def submittBttn(self, *args):  # Los controladores de eventos deben aceptar argumentos
        self.productRow.controls.append(ft.Container(
            content=ft.Column(
                [
                    ft.Text('hola')
                ]),
            margin=10,
            padding=10,
            alignment=ft.alignment.center,
            bgcolor=ft.colors.LIGHT_BLUE_ACCENT_400,
            width=200,
            height=200,
            border_radius=10,))

        self.update()

This is the Fulltraceback error:


Exception in thread Thread-79 (submittBttn):
Traceback (most recent call last):
  File "C:\Users\marct\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "C:\Users\marct\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\marct\OneDrive\Escritorio\CajaDeVenta\SellApp\VentaNa.py", line 75, in submittBttn
    self.productRow.controls.append(ft.Container(
    ^^^^^^^^^^^^^^^
AttributeError: 'ControlEvent' object has no attribute 'productRow'

The next code its a main script that runs the app.

import flet as ft

import VentaNa


def main(page: ft.Page):

    page.title = 'Venta-Na'
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    page.window_min_width = 1270  # Minimum window width in pixels
    page.window_min_height = 800  # Minimum window height in pixels

    page.update()

    # Establecer la función de cambio de tamaño como el manejador del evento on_resize

    sell = VentaNa.TopApp()
    center = VentaNa.CenterApp()
    page.add(sell)
    page.add(center)


ft.app(target=main)
0

There are 0 answers