You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flet中AlertDialog报错:'str'对象无'_set_attr_internal'属性

Flet中AlertDialog在UserControl内使用报错的解决方法

我尝试在Flet应用中创建一个继承ft.UserControl的lock类,用于激活/停用管理员模式(隐藏或显示部分元素)。该类包含一个IconButton,点击后会弹出AlertDialog验证密码,但运行代码时触发错误。

原代码

class lock(ft.UserControl):
    def __init__(self,page:ft.Page):
        super().__init__()
        self.page =page

    def on_lock_click(self,e):
        self.page.dialog = self.dlg
        self.dlg.open = True
        self.page.update()
    
    def on_dlg_activate(self,e):
        if self.entry.value == "myPw":
            print('good')
            self.dlg.open = False
        else:
            print('bad')
        self.page.update()

    def build(self):
        self.entry = ft.TextField(label='Password')
        self.dlg = ft.AlertDialog(
            title = 'Admin',
            content = self.entry,
            actions = [
                ft.ElevatedButton(
                    'log',
                    on_click= self.on_dlg_activate
                )
            ]
        )
        return ft.IconButton(
            icon= ft.icons.LOCK,
            on_click= self.on_lock_click
        )

报错信息

Exception in thread Thread-4 (on_lock_click):
Traceback (most recent call last):
  File "C:\...\Programs\Python\Python311\Lib\threading.py", line 1045, in _bootstrap_inner
    self.run()
  File "C:\...\Programs\Python\Python311\Lib\threading.py", line 982, in run
    self._target(*self._args, **self._kwargs)
  File ".\soft\src\widgets\lock.py", line 11, in on_lock_click
    self.page.update()
  File "C:\...\Local\Programs\Python\Python311\Lib\site-packages\flet_core\page.py", line 299, in update
    r = self.__update(self)
        ^^^^^^^^^^^^^^^^^^^
  File "C:\...\Local\Programs\Python\Python311\Lib\site-packages\flet_core\page.py", line 431, in __update
    commands, added_controls, removed_controls = self.__prepare_update(*controls)
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\...\Programs\Python\Python311\Lib\site-packages\flet_core\page.py", line 456, in __prepare_update
    control.build_update_commands(
  File "C:\...Local\Programs\Python\Python311\Lib\site-packages\flet_core\control.py", line 411, in build_update_commands
    ctrl.build_update_commands(
  File "C:\...\Programs\Python\Python311\Lib\site-packages\flet_core\control.py", line 423, in build_update_commands
    innerCmds = ctrl._build_add_commands(
                ^^^^^^^^^^^^^^^^^^^^^^^^^
    children = self._get_children()
               ^^^^^^^^^^^^^^^^^^^^
  File "C:\...\Local\Programs\Python\Python311\Lib\site-packages\flet_core\alert_dialog.py", line 134, in _get_children
    self.__title._set_attr_internal("n", "title")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

解决方法

报错的核心原因是:AlertDialog的title参数要求传入一个Flet Control实例(比如ft.Text),而不是直接传字符串。原代码中title='Admin'直接使用了字符串,导致Flet内部尝试调用Control的方法时出错。

修改方案是将字符串标题替换为ft.Text控件:

修改后的完整代码

class lock(ft.UserControl):
    def __init__(self,page:ft.Page):
        super().__init__()
        self.page =page

    def on_lock_click(self,e):
        self.page.dialog = self.dlg
        self.dlg.open = True
        self.page.update()
    
    def on_dlg_activate(self,e):
        if self.entry.value == "myPw":
            print('good')
            self.dlg.open = False
        else:
            print('bad')
        self.page.update()

    def build(self):
        self.entry = ft.TextField(label='Password')
        self.dlg = ft.AlertDialog(
            # 将字符串替换为ft.Text控件
            title = ft.Text('Admin'),
            content = self.entry,
            actions = [
                ft.ElevatedButton(
                    'log',
                    on_click= self.on_dlg_activate
                )
            ]
        )
        return ft.IconButton(
            icon= ft.icons.LOCK,
            on_click= self.on_lock_click
        )

内容的提问来源于stack exchange,提问作者auguste

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 00:27:08