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

使用Flet调用launch_url打开链接报错:'function' object has no attribute 'launch_url'

解决Flet中launch_url调用的AttributeError问题

错误原因

你代码里的ft.page是Flet提供的函数,用于获取当前上下文的Page实例,并非直接的Page对象。直接访问ft.page.launch_url相当于试图访问函数的属性,自然会抛出AttributeError: 'function' object has no attribute 'launch_url'。

两种解决方案

方案1:通过事件对象获取Page实例

点击事件的参数e(事件对象)自带page属性,直接调用即可:

def open_repo(e):
    e.page.launch_url('https://github.com/iqfareez/flet-hello')

方案2:调用ft.page()获取当前Page实例

如果场景中无法通过事件对象获取页面,可调用ft.page()函数获取当前上下文的Page对象:

def open_repo(e):
    current_page = ft.page()
    current_page.launch_url('https://github.com/iqfareez/flet-hello')

修改后的完整代码

import flet as ft


def main(page: ft.Page):

    page.padding = ft.Padding(20, 35, 20, 20)
    page.theme_mode = ft.ThemeMode.LIGHT

    appbar = ft.AppBar(
        title=ft.Text(value="Flutter using Flet"),
        bgcolor=ft.colors.BLUE,
        color=ft.colors.WHITE,
        actions=[ft.IconButton(icon=ft.icons.CODE, on_click=open_repo)])

    page.controls.append(appbar)

    page.update()


def open_repo(e):
    e.page.launch_url('https://github.com/iqfareez/flet-hello')


ft.app(target=main, assets_dir='assets')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 15:45:19