有建议的条目 [英] Entry with suggestions

查看:26
本文介绍了有建议的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

示例:预定义的网址:

当用户输入http://www.g"时以该字符串开头的三个 URL 将显示(以某种方式)并在键入 'http://www.goog 时显示' 以那个开头的两个要显示

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

有什么想法吗?

推荐答案

An Entry with an EntryCompletion 似乎比 ComboBoxEntry 更合适.与往常一样,教程是一个好的开始.

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

当预定义的 URL 列表很小且固定时,设置起来非常容易.你只需要填充一个 ListStore:

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

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

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)

这将测试 ListStore 中的每个值是否匹配,因此它不能扩展到巨大的列表(我的意思是巨大;1000 可以正常工作).

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).

务必使用 EntryCompletion 的各种选项,以配置最愉快的行为.

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

这篇关于有建议的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆