如何让 pygtk 条目只接受浮点数?

发布于 2024-12-03 12:55:54 字数 166 浏览 1 评论 0原文

我希望用户只能输入浮点数(即从 0 到 9 的数字,并且小数点应该显示,其他字符不应该显示)。我怎样才能做到这一点?

编辑 我需要获取像“4”或“3.5”或“.9”这样的值,像“10e23”这样的

值也应该被拒绝,例如“10.12.45”......

I want the user to be able to type only a float (that is, numbers from 0 to 9 and the decimal point should show, other characters should not). How can I do that ?

edit
I need to get values like "4" or "3.5" or ".9", nothing like "10e23"

Inconsistent values should also be rejected, such as "10.12.45" ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-12-10 12:55:54

由于

    numeric_const_pattern = r"""
    [-+]? # optional sign
    (?:
        (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
        |
        (?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
    )
    # followed by optional exponent part if desired
    (?: [Ee] [+-]? \d+ ) ?
    """
    self.rx = re.compile(numeric_const_pattern, re.VERBOSE)

init 部分中的 SO 问题“How to extract a float number from a string in Python”,

我终于得到了一个可以接受的答案,并且:

def validate_float(self, widget, entry):
    entry_text = entry.get_text()
    newtext = self.rx.findall(entry_text)
    if len(newtext) :
        entry.set_text(newtext[0])
    else:
        entry.set_text("")

连接到条目的“changed”事件。非常感谢所有提供帮助的人。

I've finally an acceptable answer thanks to SO question "How to extract a floating number from a string in Python"

    numeric_const_pattern = r"""
    [-+]? # optional sign
    (?:
        (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
        |
        (?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
    )
    # followed by optional exponent part if desired
    (?: [Ee] [+-]? \d+ ) ?
    """
    self.rx = re.compile(numeric_const_pattern, re.VERBOSE)

in the init part,

and :

def validate_float(self, widget, entry):
    entry_text = entry.get_text()
    newtext = self.rx.findall(entry_text)
    if len(newtext) :
        entry.set_text(newtext[0])
    else:
        entry.set_text("")

connected to the "changed" event of the entry. Many thanks to all those who helped.

吻安 2024-12-10 12:55:54

我不确定您是否会从“已更改”事件中获取缓冲区。

但是,查看连接到信号可能是个好主意 "preedit-changed" 在条目本身中:

使用输入法时会发出“preedit-changed”信号,
输入的文本不会立即提交到缓冲区。所以如果
您对文本感兴趣,请连接到此信号。

然后,当您获得输入时,您可以检查它是否有效,然后相应地操作字段值。

I'm not sure if you will get the buffer from the "changed" event.

However, it might be a good idea to have a look at connecting to the signal "preedit-changed" in the entry itself:

The "preedit-changed" signal is emitted when an input method is used,
the typed text will not immediately be committed to the buffer. So if
you are interested in the text, connect to this signal.

And then when you get the input you can check so that it is valid, and then manipulate the fields value accordingly.

酸甜透明夹心 2024-12-10 12:55:54

捕获当用户修改条目中的值时触发的事件,并手动验证它是浮点数。这就像在 float() 周围使用 try 一样简单:

>>> float("1.4")
1.4
>>> float("1.4a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 1.4a
>>> 

如果发生异常,则拒绝该值。

编辑:您也可以在输入字符时对其进行过滤。

if newCharacter in "0123456789.":
    # valid
else
    # invalid

Catch the event fired when the user modifies the value in the entry, and manually validate it's a float. This can be as easy as using a try around float():

>>> float("1.4")
1.4
>>> float("1.4a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 1.4a
>>> 

If an exception occurs, reject the value.

Edit: you can filter the characters as they are entered, too.

if newCharacter in "0123456789.":
    # valid
else
    # invalid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文