带有建议的条目

发布于 2024-08-20 19:42:59 字数 887 浏览 4 评论 0原文

我正在构建一个小型 PyGTK 应用程序,并且有一个文本输入字段(当前是 ComboBoxEntry),其中填充了用户应该能够从中选择的一些值。

我认为我想要做的是过滤掉匹配字段并仅显示这些字段,以便使用键盘箭头的用户可以选择匹配字段之一。

为了提供一些背景知识,预定义值是一堆 URL,用户应该能够从这些 URL 中进行选择或填写新的 URL。

例子: 预定义网址:

当用户输入 '< a href="http://www.g" rel="noreferrer">http://www.g' 以该字符串开头的三个 URL 将在输入“http://www.goog 时显示(以某种方式) ' 以此开头的两个内容将被显示

有任何想法吗?

I'm building a small PyGTK application and I have an text input field (currently a ComboBoxEntry) which is populated with a few values that the user should be able to choose from.

I think what I want to do is to filter out the matching fields and only show those ones so the user using the keyboard arrows can choose one of the matching ones.

To give some background the predefined values are a bunch of urls and the user should be able to choose from theese or fill in a new one.

Example:
the predefined urls:

When a user types 'http://www.g'
The three URLs starting with that string is to be shown (in some way) and when typeing 'http://www.goog' the two starting with that is to be shown

Any Ideas?

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

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

发布评论

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

评论(3

为你鎻心 2024-08-27 19:42:59

带有 EntryCompletionEntry 似乎比 ComboBoxEntry 更合适。一如既往,教程是一个好的开始。

当预定义的 URL 列表较小且固定时,设置非常容易。
您只需要填充一个 ListStore:

# simplified example from the tutorial
import gtk

urls = [
    'http://www.google.com',
    'http://www.google.com/android',
    'http://www.greatstuff.com',
    'http://www.facebook.com',
    ]
liststore = gtk.ListStore(str)
for s in urls:
    liststore.append([s])

completion = gtk.EntryCompletion()
completion.set_model(liststore)
completion.set_text_column(0)

entry = gtk.Entry()
entry.set_completion(completion)

# boilerplate
window = gtk.Window()
window.add(entry)

window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()
gtk.main()

用户不太可能费心输入“http://”甚至“www.”,因此您可能想要匹配 URL 的任何部分(例如,只需“og”即可!)

def match_anywhere(completion, entrystr, iter, data):
    modelstr = completion.get_model()[iter][0]
    return entrystr in modelstr
completion.set_match_func(match_anywhere, None)

:将测试 ListStore 中的每个值是否匹配,因此它无法扩展到巨大的列表(我的意思是巨大;1000 就可以了)。

请务必使用 EntryCompletion 的各种选项,以配置最令人愉快的行为。

An Entry with an EntryCompletion seems more appropriate than a ComboBoxEntry. As always, the tutorial is a good start.

It's very easy to set up when the predefined URLs list is small and fixed.
You just need to populate a ListStore:

# simplified example from the tutorial
import gtk

urls = [
    'http://www.google.com',
    'http://www.google.com/android',
    'http://www.greatstuff.com',
    'http://www.facebook.com',
    ]
liststore = gtk.ListStore(str)
for s in urls:
    liststore.append([s])

completion = gtk.EntryCompletion()
completion.set_model(liststore)
completion.set_text_column(0)

entry = gtk.Entry()
entry.set_completion(completion)

# boilerplate
window = gtk.Window()
window.add(entry)

window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()
gtk.main()

Users are not likely to bother typing "http://" or even "www.", so you probably want to match any part of the URL (e.g. just "og" works!):

def match_anywhere(completion, entrystr, iter, data):
    modelstr = completion.get_model()[iter][0]
    return entrystr in modelstr
completion.set_match_func(match_anywhere, None)

This will test every value in the ListStore for a match, so it's not scalable to huge lists (I mean huge; a 1000 works fine).

Be sure to play with the various options of EntryCompletion, to configure the most pleasant behavior.

撩发小公举 2024-08-27 19:42:59

您可能想了解一下桌面栏小程序Cuemiac做到了。

You may want to look at how Deskbar Applet's Cuemiac does it.

旧伤还要旧人安 2024-08-27 19:42:59

嗯,你显然想要处理前缀,所以你可能想要使用某种 trie。当然,还有一些问题需要处理。例如,在一个人输入了几个字母(甚至可能只是一个)后,您将希望遍历特里树的其余分支来查找建议,或者将建议存储在每个节点中。许多此类决定取决于您计划有多少可能的建议。

Well, you obviously want to deal with prefixes so you'll probably want to use some sort of trie. Of course, there are issues to deal with. For instance, after a person has typed in a few letters ( or maybe even just one) you will want to either traverse the rest of the branches of the trie to find suggestions, or have suggestions stored in each node. A lot of these sorts of decisions depend on how many possible suggestions you plan on having.

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