创建一个仅接受数字的 pygtk 文本字段

发布于 2024-08-30 04:44:45 字数 70 浏览 8 评论 0原文

有谁知道如何使用 PyGTK 创建一个只接受数字的文本字段。我正在使用 Glade 来构建我的 UI。

干杯,

Does anybody know how to create a text field using PyGTK that only accepts number. I am using Glade to build my UI.

Cheers,

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

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

发布评论

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

评论(3

遗失的美好 2024-09-06 04:44:45

我不知道有什么方法可以通过简单地切换设置来完成这样的事情,我想您需要通过信号来处理这个问题,一种方法是连接到 changed 信号,然后进行过滤输出任何不是数字的东西。

简单的方法(未经测试但应该可行):

class NumberEntry(gtk.Entry):
    def __init__(self):
        gtk.Entry.__init__(self)
        self.connect('changed', self.on_changed)

    def on_changed(self, *args):
        text = self.get_text().strip()
        self.set_text(''.join([i for i in text if i in '0123456789']))

如果您想要格式化数字,您当然可以更喜欢使用正则表达式或其他东西,以确定哪些字符应保留在条目中。

编辑
由于您可能不想在 Python 中创建条目,因此我将向您展示一种“麻木”现有条目的简单方法。

    def numbify(widget):
        def filter_numbers(entry, *args):
            text = entry.get_text().strip()
            entry.set_text(''.join([i for i in text if i in '0123456789']))

        widget.connect('changed', filter_numbers)

    # Use gtk.Builder rather than glade, you'll need to change the format of your .glade file in Glade accordingly
    builder = gtk.Builder()
    builder.add_from_file('yourprogram.glade')
    entry = builder.get_object('yourentry')

    numbify(entry)

I wouldn't know about a way to do something like this by simple switching a settings, I guess you will need to handle this via signals, one way would be to connect to the changed signal and then filter out anything that's not a number.

Simple approach(untested but should work):

class NumberEntry(gtk.Entry):
    def __init__(self):
        gtk.Entry.__init__(self)
        self.connect('changed', self.on_changed)

    def on_changed(self, *args):
        text = self.get_text().strip()
        self.set_text(''.join([i for i in text if i in '0123456789']))

If you want formatted Numbers you could of course go more fancy with a regex or something else, to determine which characters should stay inside the entry.

EDIT
Since you may not want to create your Entry in Python I'm going to show you a simple way to "numbify" an existing one.

    def numbify(widget):
        def filter_numbers(entry, *args):
            text = entry.get_text().strip()
            entry.set_text(''.join([i for i in text if i in '0123456789']))

        widget.connect('changed', filter_numbers)

    # Use gtk.Builder rather than glade, you'll need to change the format of your .glade file in Glade accordingly
    builder = gtk.Builder()
    builder.add_from_file('yourprogram.glade')
    entry = builder.get_object('yourentry')

    numbify(entry)
故事与诗 2024-09-06 04:44:45

如果您不想清理用户输入,请避免完全允许文本输入。如果您想收集小时和分钟,可以使用旋转按钮或其他小部件来限制用户的选择。

查看旋转按钮示例:
http://www.pygtk.org/pygtk2tutorial/examples/spinbutton.py

If you don't want to sanitize user input, avoid allowing text input entirely. If you're trying to collect hours and minutes, how about spin buttons or other widgets where you can limit the user's choice.

Check out the spinbutton example:
http://www.pygtk.org/pygtk2tutorial/examples/spinbutton.py

月隐月明月朦胧 2024-09-06 04:44:45

将文本转换为数字,以防它无法处理错误并将文本设置为空字符串。

您可以将其概括为按照您想要的方式匹配正则表达式

try:
    val = float(entry.get_text())
    entry.set_text(str(val))
except ValueError:
    entry.set_text('')

Convert your text to a number and in case it doesn't handle the error and set the text to an empty string.

You can generalize this to match a regular expression the way you want

try:
    val = float(entry.get_text())
    entry.set_text(str(val))
except ValueError:
    entry.set_text('')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文