bug: InputFilter number checking

未关闭
#7,098 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@ndonkoHenri 已经在做这个了。

开始于 2026年11月18日。

评估

这个 Issue 还没有评估数据。

描述

bug
Duplicate Check
Describe the bug

I want to build a menu with two options which allow the user to enter an integer value. The first option lets the user enter a value between 1 and 24 and the second option between 25 and 48. The value is entered through a text field in a dialog alert. The text field has an input filter with the relevant regex string to restrict the entry to the relevant values. The regex strings were run through regex101.com to check that they work. The menu option that allows choice between 1 and 24, with the regex string r"^(\b\d|1\d|2[0-4])$" works. The option to select between 25 and 48 with regex string r"^(\b\2[5-9]|3\d|4[0-8])$" does not work as the first digit that the user tries to enter (2, 3 or 4) does not pass the first test of the regex string. I have checked this using the regex string for the second option r"^(\b\d|[1-3]\d|4[0-8])*$" and I can enter a value of 25, but also 10, which is something I want to prevent.
I have added my main.py and associated files from the test app so that you can see what is happening.

main.py
menu.py
alert.py

Code sample
Code
import flet as ft


class Menu(ft.MenuBar):
    def __init__(
        self,
    ):
        super().__init__()
        # self.iqstixx = iqstixx
        self.expand = True
        self.style = ft.MenuStyle(
            alignment=ft.Alignment.TOP_LEFT,
            fixed_size=ft.Size.from_height(50),
            padding=2,
            bgcolor=ft.Colors.BLUE_50,
            mouse_cursor={
                ft.ControlState.HOVERED: ft.MouseCursor.WAIT,
                ft.ControlState.DEFAULT: ft.MouseCursor.ZOOM_OUT,
            },
        )

        self.controls = self.makeMenuBarItems(ft.Event[ft.SubmenuButton])

    def is_isolated(self):
        return True

    def makeMenuBarItems(self, event):
        top_menu_names = {
            "Choices": self.makeChoiceMenu(event),
        }

        top_menu = [
            ft.SubmenuButton(
                content=ft.Text(key),
                controls=value,
                style=ft.ButtonStyle(padding=ft.Padding.all(10)),
            )
            for key, value in top_menu_names.items()
        ]

        return top_menu

    def makeChoiceMenu(self, ev: ft.Event[ft.SubmenuButton]):
        func_dict = {
            "C0": self.makeChoice0,
            "C1": self.makeChoice1,
        }
        menu_back = [
            ft.MenuItemButton(
                content=ft.Text(key),
                on_click=value,
                style=ft.ButtonStyle(padding=ft.Padding.all(10)),
            )
            for key, value in func_dict.items()
        ]

        return menu_back

    def makeChoice0(self, e: ft.Event[ft.MenuItemButton]):
        self.makeChoice(self.makeChoice0, e)

    def makeChoice1(self, e: ft.Event[ft.MenuItemButton]):
        self.makeChoice(self.makeChoice1, e)

    def makeChoice(self, func_in, e):
        choice_menu = ["C1", "C2"]
        choice_dict = {
            "C1": [1, 24],
            "C2": [25, 48],
        }
        func_no = int((func_in.__name__)[-1])
        choice_range = choice_dict[choice_menu[func_no]]
        choice_question = ChoiceAlert("Select a challenge", choice_range)
        e.page.show_dialog(choice_question)


class ChoiceAlert(ft.AlertDialog):
    def __init__(self, title_text, clist):
        # dictionary defines the regex string and the max length
        # of the input string
        # 1st 48 regex string does not allow any value from 25 to 48
        # to be entered into the text field
        # 2nd 48 regex string does allow values from 1 to 48
        regex_dict = {
            24: [r"^(\b\d|1\d|2[0-4])*$", 2],
            48: [r"^(\b2[5-9]|3\d|4[0-8])*$", 2],
            # 48: [r"^(\b\d|[1-3]\d|4[0-8])*$", 2],
        }

        super().__init__()
        self.modal = True
        self.title = ft.Text(title_text)
        self.alignment = ft.Alignment.BOTTOM_RIGHT
        self.tight = True
        self.top = 300
        self.left = 300
        self.inset_padding = ft.Padding(
            top=300,
            bottom=20,
            right=20,
        )
        self.actions = [
            ft.TextButton(
                on_click=self.handle_yes_click,
                content="Enter value",
            ),
            ft.TextButton(
                on_click=self.handle_no_click,
                content="Cancel",
            ),
        ]

        self.content = ft.Column(
            controls=[
                ft.Text("Enter a value between {} and {}".format(*clist)),
                ft.TextField(
                    max_length=regex_dict[clist[1]][1],
                    value="",
                    keyboard_type=ft.KeyboardType.NUMBER,
                    input_filter=ft.InputFilter(
                        allow=True,
                        regex_string=regex_dict[clist[1]][0],
                        replacement_string="",
                    ),
                ),
            ]
        )

    def handle_yes_click(self, e: ft.Event[ft.TextField]):
        print(self.content.controls[1].value)
        e.page.pop_dialog()

    def handle_no_click(self, e: ft.Event[ft.CupertinoDialogAction]):
        e.page.pop_dialog()


def main(page: ft.Page):
    page.title = "Choice Test"
    page.window.height = 600
    page.window.width = 800

    page.window.resizable = False

    my_menu = Menu()

    page.add(ft.Column(controls=[my_menu]))
    page.update()


if __name__ == "__main__":
    ft.run(main, assets_dir="assets")
To reproduce
  1. Run app and menu "Choices" appears
  2. Select option C0 from menu listing and the alert appears. Enter a value between 1 and 24, in the text field. Press enter and the value entered is printed to a terminal window.
  3. Now select option C1 and it is not possible to enter a value into the text field.
  4. Change the regex_string in the InputFilter by commenting out line 94 (r"^(\b\2[5-9]|3\d|4[0-8])$") and uncommenting line 95 (r"^(\b\d|[1-3]\d|4[0-8])$"). A value can now be entered.
Expected behavior

Expected to be able to be able to enter and restrict the values in Choice C1 between 25 and 48 with the regex string r"^(\b\2[5-9]|3\d|4[0-8])*$".

This does not happen.

Screenshots / Videos
Captures

[Upload media here]

Operating System

macOS

Operating system details

Sonoma 14.6.1

Flet version

1.1.2

Regression

I'm not sure / I don't know

Suggestions

It would appear that your input filter looks at each individual character in the value entered rather than the whole value when running the regex test. A possible solution would be to feed and error message back to the textfield once the user has entered the value rather than prevent entry.

Logs
Logs
[Paste your logs here]
Additional details

No response

主要语言
Python
星标
17.6k
派生
753
平均合并
2 天 8 小时
30 天内合并 PR
64

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

flet-dev/flet 的其他 Issue

查看 flet-dev/flet 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。