python - 如何让 pygtk 条目只接受 float ?

标签 python pygtk

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

编辑 我需要获取诸如“4”或“3.5”或“.9”之类的值,而不是“10e23”之类的值

不一致的值也应该被拒绝,例如“10.12.45”...

最佳答案

由于问题“How to extract a float 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)

在初始化部分,

和:

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("")

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

关于python - 如何让 pygtk 条目只接受 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7318965/

相关文章:

python - pygtk 2.22和python 2.6严重不稳定

python - 处理 Celery 任务代码中的错误的推荐方法是什么?

python - 在 GTK 中,如何使窗口无法关闭?

python - For循环枚举。 Python

python - sklearn : use Pipeline in a RandomizedSearchCV?

python-2.7 - gtk : find if a widget is from some type

python - 从 GTK 获取配色方案

python - gtk.ListStore/gtk.TreeModel 和 dict 类型

python - 如何在 python 中高效地多线程或多进程处理大型 itertools.combinations?

python - 根据用户输入从 python 中的 .txt 文件中提取信息