0

0

使用 Flet 在 Python Banner 中动态显示文本的教程

霞舞

霞舞

发布时间:2025-07-16 16:24:02

|

977人浏览过

|

来源于php中文网

原创

使用 flet 在 python banner 中动态显示文本的教程

本文介绍了在使用 Flet 构建 Python 应用时,如何在 Banner 组件中动态显示不同的文本信息。通过示例代码,详细讲解了两种实现方案:直接在条件判断语句中创建 Banner 对象,以及使用 UserControl 类封装 Banner 组件。帮助开发者更灵活地控制 Banner 的显示内容,提升用户体验。

在使用 Flet 构建用户界面时,Banner 组件是一种非常有用的方式,用于向用户显示重要的信息或警告。然而,有时我们需要根据不同的情况在 Banner 中显示不同的文本内容。本文将介绍两种在 Flet 应用中实现这一目标的方法。

方法一:直接在条件语句中创建 Banner

最初的尝试是在函数外部定义一个 status_banner 变量,然后在条件语句中修改其值。但由于作用域的问题,这种方法无法正确更新 Banner 中的文本。一个有效的解决方案是在每个条件语句中直接创建 ft.Banner 对象。

以下代码展示了如何在 merge_pdfs 函数中,根据不同的条件创建并显示不同的 Banner。

立即学习Python免费学习笔记(深入)”;

import flet as ft

def main(page: ft.page):

    def close_banner(e):
        page.banner.open = False
        page.update()

    def show_banner(e):
        page.banner.open = True
        page.update()

    def merge_pdfs(e: ft.FilePickerResultEvent):

        # get file name and password from the corresponding textfields
        merge_file_name = textField_name.value
        file_password = textField_password1.value

        # show warning when no filename is provided
        if not merge_file_name or merge_file_name == ' ':
            # banner for when there is error in file name or file selection
            page.banner = ft.Banner(
                bgcolor=ft.colors.RED_500,
                leading=ft.Icon(ft.icons.WARNING_AMBER_ROUNDED,
                                color=ft.colors.AMBER, size=40),
                content=ft.Text("Please check the file name entered."),
                actions=[ft.TextButton("Dismiss", on_click=close_banner)])
            show_banner(e)
            return None

        # show warning if less than 2 files selected
        if not e.files or len(e.files) < 2:
            # banner for when there is error in file name or file selection
            page.banner = ft.Banner(
                bgcolor=ft.colors.RED_500,
                leading=ft.Icon(ft.icons.WARNING_AMBER_ROUNDED,
                                color=ft.colors.AMBER, size=40),
                content=ft.Text("Please select at least 2 files."),
                actions=[ft.TextButton("Dismiss", on_click=close_banner)])
            show_banner(e)
            return None

    pick_files_dialog = ft.FilePicker(on_result=merge_pdfs)
    page.overlay.append(pick_files_dialog)

    # 创建文本字段和按钮等其他控件...
    textField_name = ft.TextField(label="File Name")
    textField_password1 = ft.TextField(label="Password")
    pick_files_button = ft.ElevatedButton(
        "Select files",
        icon=ft.icons.UPLOAD_FILE,
        on_click=lambda _: pick_files_dialog.pick_files(allow_multiple=True),
    )

    page.add(textField_name, textField_password1, pick_files_button)

    # banner for when there is error in file name or file selection
    page.banner = ft.Banner(
        bgcolor=ft.colors.RED_500,
        leading=ft.Icon(ft.icons.WARNING_AMBER_ROUNDED,
                        color=ft.colors.AMBER, size=40),
        content=ft.Text(""),
        actions=[ft.TextButton("Dismiss", on_click=close_banner)])

if __name__ == "__main__":
    ft.app(target=main)

在这个方法中,当文件名为空或选择的文件少于两个时,会创建一个包含相应错误信息的 Banner 对象,并将其赋值给 page.banner。然后调用 show_banner(e) 函数来显示 Banner。

注意事项:

电力公司企业网站(Zblog内核)1.8
电力公司企业网站(Zblog内核)1.8

由于我高估了大家对zblog程序的熟知度,发现还有很多站长并不是太熟悉这款程序,甚至连后台的登陆入口都不清楚。所以我晚上抽了一点点时间把该ZBLOG企业网站源码进行的修正,补充了大家的一些问题。并且我写了比较详细的使用教程,能够帮助新手朋友修改变成自己的企业网站使用。 修订版本改进了几处问题: 第一,修正了单页面中的顶部BANNER FLASH幻灯图片的显示错误问题; 第二,修正了在产品中心标题显

下载
  • 这种方法需要在每个条件语句中重复创建 Banner 对象,代码略显冗余。
  • 需要确保 close_banner 函数能够正确关闭 Banner。

方法二:使用 UserControl 类封装 Banner

为了避免代码重复,可以使用 Flet 的 UserControl 类来封装 Banner 组件。这样可以将 Banner 的创建和显示逻辑封装在一个类中,并在需要时实例化该类。

以下代码展示了如何使用 UserControl 类来创建可重用的 Banner 组件。

import flet as ft

class Banner_Warning(ft.UserControl):
    def __init__(self, text_banner: str) -> None:
        super().__init__()
        self.text_banner = text_banner

    def close_banner(self, e: ft.ControlEvent) -> None:
        self.banner.open = False
        self.update()

    def build(self) -> ft.Banner:
        self.banner = ft.Banner(
            bgcolor=ft.colors.RED_500,
            leading=ft.Icon(ft.icons.WARNING_AMBER_ROUNDED,
                            color=ft.colors.AMBER, size=40),
            content=ft.Text(self.text_banner),
            actions=[ft.TextButton("Dismiss", on_click=self.close_banner)])
        self.banner.open = True
        return self.banner

def main(page: ft.page):

    def merge_pdfs(e: ft.FilePickerResultEvent):
        # get file name and password from the corresponding textfields
        merge_file_name = textField_name.value
        file_password = textField_password1.value

        # show warning when no filename is provided
        if not merge_file_name or merge_file_name == ' ':
            # banner for when there is error in file name or file selection
            page.add(Banner_Warning("Please check the file name entered."))
            return None

        # show warning if less than 2 files selected
        if not e.files or len(e.files) < 2:
            # banner for when there is error in file name or file selection
            page.add(Banner_Warning("Please select at least 2 files."))
            return None

    pick_files_dialog = ft.FilePicker(on_result=merge_pdfs)
    page.overlay.append(pick_files_dialog)

    # 创建文本字段和按钮等其他控件...
    textField_name = ft.TextField(label="File Name")
    textField_password1 = ft.TextField(label="Password")
    pick_files_button = ft.ElevatedButton(
        "Select files",
        icon=ft.icons.UPLOAD_FILE,
        on_click=lambda _: pick_files_dialog.pick_files(allow_multiple=True),
    )

    page.add(textField_name, textField_password1, pick_files_button)


if __name__ == "__main__":
    ft.app(target=main)

在这个方法中,Banner_Warning 类继承自 ft.UserControl,并在 build 方法中创建 Banner 对象。 __init__ 构造函数接收 text_banner 参数,用于设置 Banner 中显示的文本。 在 merge_pdfs 函数中,只需要实例化 Banner_Warning 类,并将相应的错误信息作为参数传递给构造函数,然后通过 page.add 将其添加到页面中。

优点:

  • 代码更加简洁,易于维护。
  • Banner 组件可以复用,减少代码重复。

总结:

本文介绍了两种在 Flet 应用中动态显示 Banner 文本的方法。第一种方法直接在条件语句中创建 Banner 对象,简单直接,但代码冗余。第二种方法使用 UserControl 类封装 Banner 组件,代码更加简洁,易于维护和复用。开发者可以根据自己的需求选择合适的方法。使用 UserControl 封装组件是更推荐的做法,能够提高代码的可维护性和可读性。

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

715

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

625

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

739

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

617

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1235

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

575

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

697

2023.08.11

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.6万人学习

Django 教程
Django 教程

共28课时 | 2.6万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号