如何在Flet中显示AlertDialog

问题描述 投票:0回答:1

在相反的代码中,我想显示一个 AlertDialog,但遇到了问题。谁能帮我?如果需要的话我也可以发送完整的代码

import flet as ft
class Task(ft.UserControl):
    def __init__(self, task_name, task_status_change, task_delete):
        super().__init__()
        self.completed = False
        self.task_name = task_name
        self.task_status_change = task_status_change
        self.task_delete = task_delete
        ...
    def build(self):
        ...
class TodoApp(ft.UserControl):
    def build(self):
        return ft.Column(
            controls=[
                ft.Row(
                    [
                        ft.ElevatedButton(text="button",on_click=self.route_change),
                    ],
                    alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                ),
            ],
        )
    async def route_change(self, e):

        dlg_modal = ft.AlertDialog(
            title=ft.Text("login"),
            content=ft.Text("asasasas"),
            actions=[
                ft.TextButton("Yes"),
                ft.TextButton("No"),
            ],
            actions_alignment=ft.MainAxisAlignment.END,
        )
        self.dialog = dlg_modal
        dlg_modal.open = True
        self.update()
async def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    page.scroll = ft.ScrollMode.ADAPTIVE
    await page.add_async(TodoApp())
ft.app(main)

当我点击按钮时,没有任何反应

python flet
1个回答
0
投票

我认为错误来自

TodoApp.route_change
函数。 将 AlertDialog 存储在
dlg_modal
变量中后,只需调用
await e.page.show_dialog_async(dlg_modal)
。 (评论/删除下面剩余的代码)

async def route_change(self, e):
   dlg_modal = ft.AlertDialog(
        title=ft.Text("login"),
        content=ft.Text("asasasas"),
        actions=[
            ft.TextButton("Yes"),
            ft.TextButton("No"),
        ],
        actions_alignment=ft.MainAxisAlignment.END,
    )
   await e.page.show_dialog_async(dlg_modal)

如果上述建议不起作用,请分享完整代码。

© www.soinside.com 2019 - 2024. All rights reserved.