我正在使用熔炉,我希望我的应用程序在点击按钮时启动一个链接。
根据文档,我可以使用launch_url
方法。但当我尝试时,我得到了以下错误:
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'
代码
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')
发布于 2022-12-02 21:14:58
要修复所获得的错误,需要从flet库导入页面模块,然后创建页面类的实例。然后,您可以调用该实例上的launch_url方法在默认的web浏览器中打开一个URL。
下面是您如何更新代码以做到这一点:
进口flet作为ft
从flet库导入页面模块
从flet导入页面
def (页面: ft.Page):
# 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')
发布于 2022-12-03 19:41:47
从我在这里看到的,以及你所得到的错误来看,你可能在Flet的安装上有问题。尝试在虚拟环境中安装并查看是否发生了更改。
祝好运
https://stackoverflow.com/questions/74661326
复制相似问题