how to use two text display options in a banner using flet in python?

152 Views Asked by At

i am trying to create an app that merges one or more pdf files in python using flet, my code below shows an empty banner display text when i run the code, i tested the two if statement inside the merge_pdfs function but it always display an empty status_banner

status_banner = ''


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 == ' ':
            status_banner = "Please check the file name entered."
            show_banner(e)
            return None

        # show warning if less than 2 files selected
        if not e.files or len(e.files) < 2:
            status_banner = "Please select at least 2 files."
            show_banner(e)
            return None

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

    ...

    # 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(status_banner),
        actions=[ft.TextButton("Dismiss", on_click=close_banner)])
1

There are 1 best solutions below

0
On

a workaround solution, putting the banner inside the 2 if statements, if anyone has a better solution im open to it.

    # 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

update 2 using a class

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):
        ...
        # 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