首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Flet中启动URL

在Flet中启动URL
EN

Stack Overflow用户
提问于 2022-12-02 20:52:37
回答 2查看 27关注 0票数 0

我正在使用熔炉,我希望我的应用程序在点击按钮时启动一个链接。

根据文档,我可以使用launch_url方法。但当我尝试时,我得到了以下错误:

代码语言:javascript
运行
复制
Exception in thread Thread-6 (open_repo):
Traceback (most recent call last):
  File "C:\Users\Iqmal\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\Users\Iqmal\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "d:\Iqmal\Documents\Python Projects\flet-hello\main.py", line 58, in open_repo
    ft.page.launch_url('https://github.com/iqfareez/flet-hello')
    ^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute 'launch_url'

代码

代码语言:javascript
运行
复制
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):
    ft.page.launch_url('https://github.com/iqfareez/flet-hello')


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

回答 2

Stack Overflow用户

发布于 2022-12-02 21:14:58

要修复所获得的错误,需要从flet库导入页面模块,然后创建页面类的实例。然后,您可以调用该实例上的launch_url方法在默认的web浏览器中打开一个URL。

下面是您如何更新代码以做到这一点:

进口flet作为ft

从flet库导入页面模块

从flet导入页面

def (页面: ft.Page):

代码语言:javascript
运行
复制
# Create a new page object
my_page = page.Page()

# Set the padding and theme mode for the page
my_page.padding = ft.Padding(20, 35, 20, 20)
my_page.theme_mode = ft.ThemeMode.LIGHT

# Create an app bar and add it to the page
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)])

my_page.controls.append(appbar)

# Update the page to display the changes
my_page.update()

def open_repo(e):#使用launch_url方法在默认的web浏览器my_page.launch_url('https://github.com/iqfareez/flet-hello')中打开URL

使用主函数作为目标运行应用程序

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

票数 0
EN

Stack Overflow用户

发布于 2022-12-03 19:41:47

从我在这里看到的,以及你所得到的错误来看,你可能在Flet的安装上有问题。尝试在虚拟环境中安装并查看是否发生了更改。

祝好运

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74661326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档