flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.5k stars 449 forks source link

AlertDialog cannot close #4345

Closed xiewencan closed 1 week ago

xiewencan commented 1 week ago

Duplicate Check

Describe the bug

when I set 'self.page.dialog = self.success', env though success.open is false,it will show a alertdialog. image

Code sample

Code ```python [Paste your code here] class success_alert(ft.AlertDialog): def __init__(self,page): super().__init__() self.modal = True self.page = page self.title = ft.Text("操作成功") self.actions = [ ft.TextButton("确定", on_click=self.close_dlg), ft.TextButton("取消", on_click=self.close_dlg), ] self.actions_alignment = ft.MainAxisAlignment.END def close_dlg(self,e): self.open = False self.page.update() class function_container(ft.Container): def __init__(self,page): super().__init__() self.add_user_alert = add_user_Alert(page) self.content = ft.Row( alignment=ft.MainAxisAlignment.CENTER, spacing=25, controls=[ ft.ElevatedButton("添加用户",on_click=self.add_user ), ft.ElevatedButton("返回登录界面", ), ft.ElevatedButton("备份", ), ] ) def add_user(self,e): self.page.dialog = self.add_user_alert self.add_user_alert.open = True self.page.update() class add_user_Alert(ft.AlertDialog): def __init__(self,page): super().__init__() self.modal = True self.success = success_alert(page) self.title = ft.Text("请输入待添加人员的消息") self.id = ft.TextField(hint_text="请输入账号", autofocus=True) self.name = ft.TextField(hint_text="请输入姓名", autofocus=True) self.password = ft.TextField(hint_text="请输入密码", autofocus=True) self.admin = ft.TextField(hint_text="请输入身份,1为普通,0为管理员", autofocus=True) self.content =(ft.Container(ft.Column(controls=[self.id,self.name, self.password, self.admin]))) self.actions = [ ft.TextButton("确定", on_click=self.certain_dlg), ft.TextButton("取消", on_click=self.close_dlg), ] def close_dlg(self,e): self.open = False self.page.update() def certain_dlg(self,e): register_sql = f"insert into card values({int(self.id.value)},'{self.name.value}',0,'{self.admin.value}','{self.password.value}') " root_cursor.execute(register_sql) root_db.commit() self.id.value = None self.name.value = None self.password.value = None self.admin.value = None self.close_dlg(e) **self.page.dialog = self.success # this point self.page.update()** ```

To reproduce

import flet as ft import global_var from db_config import * class input_container(ft.Container): def init(self): super().init() self.border = ft.border.all(1) self.border_radius = ft.border_radius.all(20) self.width = 350 self.height = 350

    self.ISBN = ft.TextField(hint_text="请输入ISBN码", width=300, autofocus=True)
    self.book_name = ft.TextField(hint_text="请输入Title", width=300)
    self.writer = ft.TextField(hint_text="请输入Author", width=300)
    self.quantity = ft.TextField(hint_text="请输入Total", width=300)
    self.content= ft.Column(
                    controls=[
                        ft.Text("请输入需要输入的消息"),
                        self.ISBN,
                        self.book_name,
                        self.writer,
                        self.quantity],
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER,
                    alignment=ft.MainAxisAlignment.CENTER,
                    spacing=10)

class table_container(ft.Container): def init(self, page): super().init() self.width = 600 self.height = 350 self.border = ft.border.all(1) self.border_radius = 20 self.data =[[]] self.col_names =[] self.table_init() self.content = ft.ListView( controls=[ self.table, ], expand=20, spacing=10, padding=20, auto_scroll=True, ) def table_init(self): table = ft.DataTable( columns=[ ft.DataColumn( ft.Text(col)) for col in self.col_names], rows=[ ft.DataRow( cells=[ ft.DataCell( ft.Text(cell.replace(" ","") if isinstance(cell,str) else cell)) for cell in row]) for row in self.data], data_row_max_height=40, column_spacing=10, ) self.table = table

class button_container(ft.Container): def init(self,page,table_container:table_container): super().init() self.container = table_container self.border = ft.border.all(1) self.border_radius = ft.border_radius.all(10) self.width = 100 self.height = 350 self.col_names = [] self.data =[[]] self.add_buttom = ft.ElevatedButton(text="add", ) self.delete_buttom = ft.ElevatedButton(text="delete", ) self.upadate_buttom = ft.ElevatedButton(text="update", ) self.select_buttom = ft.ElevatedButton(text="select",) self.content = ft.Column( controls=[ ft.Text("请选择操作"), self.add_buttom, self.delete_buttom, self.upadate_buttom, self.select_buttom], horizontal_alignment=ft.CrossAxisAlignment.CENTER, alignment=ft.MainAxisAlignment.CENTER, spacing=30 ) def add(self,e): sql1 = f"insert into books values('{self.ISBN.value}','{self.book_name.value}','{self.writer.value}',{self.quantity.value},{self.quantity.value})" root_cursor.execute(sql1) root_db.commit() sql2 = f"select * from books where ISBN='{self.ISBN.value}'" root_cursor.execute(sql2)

update_data = root_cursor.fetchall()

    # admin_update(e)

    search_count = f"select count(*) from book"
    root_cursor.execute(search_count)
    count = root_cursor.fetchall()[0][0]
    for i in range(int(self.quantity.value)):
        add_sql = f"insert into book values({count + i + 1},'{self.ISBN.value}',1)"
        root_cursor.execute(add_sql)
        root_db.commit()

def delete(self,e):
    sql2 = f"delete from book where ISBN='{self.ISBN.value}'"
    root_cursor.execute(sql2)
    root_db.commit()
    sql1 = f"delete from books where ISBN='{self.ISBN.value}'"
    root_cursor.execute(sql1)
    root_db.commit()
    sql2 = f"select * from books"
    root_cursor.execute(sql2)
    # update_data = root_cursor.fetchall()
    # admin_update(e)

def update(self,e):
    global root_cursor, root_db, update_data, update_col_names
    search_count = f"select Total from books where ISBN='{self.ISBN.value}'"
    root_cursor.execute(search_count)
    count = root_cursor.fetchall()[0][0]
    sql1 = f"update books set Title='{self.book_name.value}',Author='{self.writer.value}',Total={self.quantity.value} where ISBN='{self.ISBN.value}'"
    root_cursor.execute(sql1)
    root_db.commit()
    search_kc = f"select Available from books where ISBN='{self.ISBN.value}'"
    root_cursor.execute(search_kc)
    kcount = root_cursor.fetchall()[0][0]
    sql3 = f"select count(*) from book"
    root_cursor.execute(sql3)
    base = root_cursor.fetchall()[0][0]
    print(count, self.quantity.value)
    if count < int(self.quantity.value):
        print("已执行1")
        for i in range(int(self.quantity.value) - count):
            add_sql = f"insert into book values({base + i + 1},'{self.ISBN.value}',1)"
            root_cursor.execute(add_sql)
            root_db.commit()
        update_k = f"update books set Available={kcount + int(self.quantity.value) - count} where ISBN='{self.ISBN.value}'"
        root_cursor.execute(update_k)
        root_db.commit()
    elif count > int(self.quantity.value):
        print("已执行2")
        for i in range(count - int(self.quantity.value)):
            delete_sql = f"delete from book where ISBN='{self.ISBN.value}' and Borrowable=1 limit 1"
            root_cursor.execute(delete_sql)
            root_db.commit()
        update_k = f"update books set Available={kcount + int(self.quantity.value) - count} where ISBN='{self.ISBN.value}'"
        root_cursor.execute(update_k)
        root_db.commit()
    else:
        pass

    sql2 = f"select * from books where ISBN='{self.ISBN.value}'"
    root_cursor.execute(sql2)
    # update_data = root_cursor.fetchall()
    # update_col_names = [desc[0] for desc in root_cursor.description]
    # admin_update(e)

def select(self,e):
    global root_cursor, root_db, update_data, update_col_names
    if self.ISBN.value != "":
        sql1 = f"select * from books where ISBN='{self.ISBN.value}'"
    elif self.book_name.value != "":
        sql1 = f"select * from books where Title='{self.book_name.value}'"
    elif self.writer.value != "":
        sql1 = f"select * from books where Author='{self.writer.value}'"
    else:
        sql1 = f""
    print(sql1)
    root_cursor.execute(sql1)
    update_col_names = [desc[0] for desc in root_cursor.description]
    # update_data = root_cursor.fetchall()
    # print(update_data)
    # admin_update(e)

def admin_update(self,e):
    new_columns = [
        ft.DataColumn(
            ft.Text(col)) for col in self.col_names]
    new_rows = [
        ft.DataRow(
            cells=[
                ft.DataCell(
                    ft.Text(
                        cell.replace(
                            " ",
                            "") if isinstance(
                            cell,
                            str) else cell)) for cell in row]) for row in self.data]

    self.container.table.rows = new_rows
    self.container.table.columns = new_columns
    self.page.update()

class success_alert(ft.AlertDialog): def init(self,page): super().init() self.modal = True self.page = page self.title = ft.Text("操作成功") self.actions = [ ft.TextButton("确定", on_click=self.close_dlg), ft.TextButton("取消", on_click=self.close_dlg), ] self.actions_alignment = ft.MainAxisAlignment.END def close_dlg(self,e): self.open = False self.page.update() class welcome_container(ft.AlertDialog): def init(self,page): super().init() self.modal = True self.title = ft.Text("欢迎") self.content = ft.Text(f"管理员:,成功登录") self.actions = [ ft.TextButton("确定",on_click=self.close_welcome) , ft.TextButton("取消", on_click=self.close_welcome), ] self.actions_alignment = ft.MainAxisAlignment.END self.on_dismiss = lambda e: print("Modal dialog dismissed!")

def close_welcome(self,e):
    self.open = False
    self.page.update()

# def open_welcome(self,e):
#     self.page.dialog = self.welcome
#     self.welcome.open = True
#     self.page.update()

class add_user_Alert(ft.AlertDialog): def init(self,page): super().init() self.modal = True self.success = success_alert(page) self.title = ft.Text("请输入待添加人员的消息") self.id = ft.TextField(hint_text="请输入账号", autofocus=True) self.name = ft.TextField(hint_text="请输入姓名", autofocus=True) self.password = ft.TextField(hint_text="请输入密码", autofocus=True) self.admin = ft.TextField(hint_text="请输入身份,1为普通,0为管理员", autofocus=True) self.content =(ft.Container(ft.Column(controls=[self.id,self.name, self.password, self.admin]))) self.actions = [ ft.TextButton("确定", on_click=self.certain_dlg), ft.TextButton("取消", on_click=self.close_dlg), ] def close_dlg(self,e): self.open = False self.page.update() def certain_dlg(self,e): register_sql = f"insert into card values({int(self.id.value)},'{self.name.value}',0,'{self.admin.value}','{self.password.value}') " root_cursor.execute(register_sql) root_db.commit() self.id.value = None self.name.value = None self.password.value = None self.admin.value = None self.close_dlg(e) self.page.dialog = self.success self.page.update()

class function_container(ft.Container): def init(self,page): super().init() self.add_user_alert = add_user_Alert(page) self.content = ft.Row( alignment=ft.MainAxisAlignment.CENTER, spacing=25, controls=[ ft.ElevatedButton("添加用户",on_click=self.add_user ), ft.ElevatedButton("返回登录界面", ), ft.ElevatedButton("备份", ), ] ) def add_user(self,e): self.page.dialog = self.add_user_alert self.add_user_alert.open = True self.page.update()

class admin_view(ft.View): def init(self,page): super().init() self.route = "/admin"

    self.input_container = input_container()
    self.table_container = table_container(page)
    self.buttom_container = button_container(page,self.table_container)
    self.function_container = function_container(page)
    self.controls = [ft.Row(
        alignment=ft.MainAxisAlignment.CENTER,
        controls=[
            self.input_container,
            self.buttom_container,
            self.table_container,
        ]),self.function_container]

# page.dialog = welcome
# welcome.open = True
# page.update()
#
#
# def close_welcome(e):
#     welcome.open = False
#     page.update()
# 表格显示-------------------------------------------------------------

#

#

#
# page.title = "AlertDialog examples"
#
# def close_dlg(e):
#     dlg_modal.open = False
#     print(students.value, name.value)
#     page.update()
#
# def certain_dlg(e):
#     global root_cursor
#     global root_db
#     register_sql = f"insert into card values({int(students.value)},'{name.value}',0,'{admin.value}','{password.value}') "
#     root_cursor.execute(register_sql)
#     root_db.commit()
#     students.value = None
#     name.value = None
#     password.value = None
#     admin.value = None
#     close_dlg(e)
#     welcome.content = ft.Text("添加成功")
#     page.dialog = welcome
#     welcome.open = True
#     page.update()
#
#
# dlg_modal = ft.AlertDialog(
#     modal=True,
#     title=ft.Text("请输入待添加人员的消息"),
#     content=,
#     actions=[
#         ft.TextButton("确定", on_click=certain_dlg),
#         ft.TextButton("取消", on_click=close_dlg),
#     ],
#     actions_alignment=ft.MainAxisAlignment.END,
#     on_dismiss=lambda e: print("Modal dialog dismissed!"),
# )
#
# def open_dlg_modal(e):
#     page.dialog = dlg_modal
#     dlg_modal.open = True
#     page.update()
#
#
#
# def backup(e):
#     flag = os.system('mysqldump -uroot -p[请输入你的密码] BMS > d:bms.sql')
#     if flag == 0:
#         print("备份成功")
#         welcome.content = ft.Text("备份成功")
#     else:
#         print("备份失败")
#         welcome.content = ft.Text("备份失败")
#     welcome.open = True
#     page.update()
#
# # page.add(ft.Row(controls=[ft.Column(controls=[ISBN,book_name,writer,quantity],ft.Column(controls=[add_buttom,delete_buttom,upadate_buttom,select_buttom])]))
# page.add(
#     ft.AppBar(
#         title=ft.Row(
#             controls=[
#                 ft.Text(
#                     f"{real_name}"),
#             ]),
#         bgcolor=ft.colors.SURFACE_VARIANT),
# )
# page.add(
#

def main(page: ft.Page): print("hello world") page.title = "Routes Example"

def route_change(route):
    vi = admin_view(page)
    page.views.clear()
    page.views.append(
        vi
    )
    if page.route == "/store":
        page.views.append(
            ft.View(
                "/store",
                [
                    ft.AppBar(title=ft.Text("Store"), bgcolor=ft.colors.SURFACE_VARIANT),
                    ft.ElevatedButton("Go Home", on_click=lambda _: page.go("/")),
                ],
            )
        )
    page.update()

def view_pop(view):
    page.views.pop()
    top_view = page.views[-1]
    page.go(top_view.route)

page.on_route_change = route_change
page.on_view_pop = view_pop
page.go(page.route)

ft.app(target=main)

Expected behavior

No response

Screenshots / Videos

Captures [Upload media here]

Operating System

Windows

Operating system details

windows11 24h2

Flet version

0.10.2

Regression

No, it isn't

Suggestions

No response

Logs

Logs ```console [Paste your logs here] ```

Additional details

No response

ndonkoHenri commented 1 week ago

Please share well formated runnable code to reproduce the issue.