如何过滤或限制在 PyGTK 文本输入字段中输入的文本?

发布于 2024-10-19 13:13:15 字数 50 浏览 3 评论 0原文

我想要一个文本输入字段(gtk.Entry),它只接受十六进制字符作为用户的有效输入。

I want a text entry field (gtk.Entry) that accepts only hexadecimal characters as valid input from the user.

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

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

发布评论

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

评论(1

初熏 2024-10-26 13:13:15

可以通过连接到“insert_text”信号并在信号处理程序中操作输入的文本来完成过滤。
以下是验证十六进制字符的示例代码:

#!/usr/bin/env python
import gtk, pygtk, gobject, string

class HexEntry(gtk.Entry):
    """A PyGTK text entry field which allows only Hex characters to be entered"""

    def __init__(self):
        gtk.Entry.__init__(self)
        self.connect("changed", self.entryChanged)
        self.connect("insert_text", self.entryInsert)

    def entryChanged(self, entry):
        # Called only if the entry text has really changed.
        print "Entry changed"

    def entryInsert(self, entry, text, length, position):
        # Called when the user inserts some text, by typing or pasting.
        print "Text inserted"

        position = entry.get_position() # Because the method parameter 'position' is useless

        # Build a new string with allowed characters only.
        result = ''.join([c for c in text if c in string.hexdigits])

        # The above line could also be written like so (more readable but less efficient):
        # result = ''
        # for c in text:
        #     if c in string.hexdigits:
        #         result += c

        if result != '':
            # Insert the new text at cursor (and block the handler to avoid recursion).
            entry.handler_block_by_func(self.entryInsert)
            entry.insert_text(result, position)
            entry.handler_unblock_by_func(self.entryInsert)

            # Set the new cursor position immediately after the inserted text.
            new_pos = position + len(result)

            # Can't modify the cursor position from within this handler,
            # so we add it to be done at the end of the main loop:
            gobject.idle_add(entry.set_position, new_pos)

        # We handled the signal so stop it from being processed further.
        entry.stop_emission("insert_text")

def main():
    window = gtk.Window()
    window.connect("delete-event", gtk.main_quit)
    entry = HexEntry()
    window.add(entry)
    window.show_all()
    gtk.main()

if __name__ == "__main__":
    main()

Filtering may be done by connecting to the 'insert_text' signal and manipulating the entered text in the signal handler.
Here is example code for validating Hex characters:

#!/usr/bin/env python
import gtk, pygtk, gobject, string

class HexEntry(gtk.Entry):
    """A PyGTK text entry field which allows only Hex characters to be entered"""

    def __init__(self):
        gtk.Entry.__init__(self)
        self.connect("changed", self.entryChanged)
        self.connect("insert_text", self.entryInsert)

    def entryChanged(self, entry):
        # Called only if the entry text has really changed.
        print "Entry changed"

    def entryInsert(self, entry, text, length, position):
        # Called when the user inserts some text, by typing or pasting.
        print "Text inserted"

        position = entry.get_position() # Because the method parameter 'position' is useless

        # Build a new string with allowed characters only.
        result = ''.join([c for c in text if c in string.hexdigits])

        # The above line could also be written like so (more readable but less efficient):
        # result = ''
        # for c in text:
        #     if c in string.hexdigits:
        #         result += c

        if result != '':
            # Insert the new text at cursor (and block the handler to avoid recursion).
            entry.handler_block_by_func(self.entryInsert)
            entry.insert_text(result, position)
            entry.handler_unblock_by_func(self.entryInsert)

            # Set the new cursor position immediately after the inserted text.
            new_pos = position + len(result)

            # Can't modify the cursor position from within this handler,
            # so we add it to be done at the end of the main loop:
            gobject.idle_add(entry.set_position, new_pos)

        # We handled the signal so stop it from being processed further.
        entry.stop_emission("insert_text")

def main():
    window = gtk.Window()
    window.connect("delete-event", gtk.main_quit)
    entry = HexEntry()
    window.add(entry)
    window.show_all()
    gtk.main()

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