[Question] Open a AlertDialog in a UserControl #1940
Answered
by
ingdesarr1
ingdesarr1
asked this question in
Q&A
-
QuestionGood afternoon. In the moment, I'm create a bar, like a UserControl, and inside, I have a text and two buttons. I want to use the button to create an AlertDialog to show a form, with some spaces to fill with name, etc, and then, with other button inside the AlertDialog, save the info.(For now, i just print the info). But, the AlertDialog needs a page, and update the page, and my main page is not call inside the UserControl. How can I do that? Let the code below. Code sample*****Main Code******
import flet as ft
def main(page:ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.go('/users')
ft.app(target=main, assets_dir="assets")
******** Users code **********
import flet as ft
from form import UserForm
def _view_(page):
content=ft.Column(
controls=[
UserHeader(),
ft.Column(
expand=True,
scroll='HIDDEN',
alignment=ft.alignment.center,
controls=[
ft.Text("Pagina de usuarios"),
],
),
]
)
return content
***** UserHeader code *******
import flet as ft
from btn_save import return_form_button
class UserHeader(ft.UserControl):
def __init__(self):
super().__init__()
def app_header_text(self):
return ft.Container(
ft.Text("Usuarios",size=30),
width=200,
height=float("inf"),
)
def app_header_search(self):
return ft.Container( ##3.Controls[1] -> a
content=ft.Row( ##4.Content
controls=[
ft.Icon(name=ft.icons.SEARCH_ROUNDED,size=25,),
ft.TextField(
hint_text="Buscar",
border_color="transparent",
expand=True,) ##5.Controls[1].value
],
spacing=10,
vertical_alignment=ft.CrossAxisAlignment.CENTER
),
expand=True,
height=float("inf"),
border_radius=15,
bgcolor="blue50",
)
def text_search_bar(self,e):
if e.data == 'false':
pass
else:
self.controls[0].content.controls[1].content.controls[1].value = ""
self.controls[0].content.controls[1].update()
def build(self):
return ft.Container( ##1.Controls[0]
content=ft.Row( ##2.Content
controls=[
self.app_header_text(),
self.app_header_search(), ##3.Controls[1] ->a
return_form_button("Crear"), ##boton para guardar informacion
ft.ElevatedButton(text="Eliminar",width=120,height=70),
],
),
height=50,
on_click=lambda e: self.text_search_bar(e),
)
****** user_button Code ******
import flet as ft
from form import UserForm
def show_form(self):
ft.Page.dialog = access_data
access_data.open = True
ft.Page.update(self)
def close_tag(self):
access_data.open = False
ft.Page.update(self)
access_data = ft.AlertDialog(
modal=True,
title=ft.Text("Creacion de usuario"),
content=ft.Container(
UserForm(),
),
actions=[
ft.TextButton("Cerrar",on_click=close_tag)
],
actions_alignment=ft.MainAxisAlignment.CENTER
)
def return_form_button(name:str):
return ft.ElevatedButton(
text=name,
width=100,
height=70,
on_click=show_form,
) Error messageException in thread Thread-7 (show_form):
Traceback (most recent call last):
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "C:\Informacion\ConcretX\data_users\btn_save.py", line 17, in show_form
ft.Page.update(self)
File "C:\Informacion\ConcretX\concretx\Lib\site-packages\flet_core\page.py", line 282, in update
with self.__lock:
^^^^^^^^^^^
AttributeError: 'ControlEvent' object has no attribute '_Page__lock' ------------------------------------------------------
|
Beta Was this translation helpful? Give feedback.
Answered by
ingdesarr1
Oct 11, 2023
Replies: 1 comment
-
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ingdesarr1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#73