def get_word():
s = ""
while True:
a = getkey()
if a == "\n":
break
elif a == "\t":
print "all possibilities"
else:
s += a
return s
word = get_word()
print word
import readline
class MyCompleter(object): # Custom completer
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
if state == 0: # on first trigger, build possible matches
if text: # cache matches (entries that start with entered text)
self.matches = [s for s in self.options
if s and s.startswith(text)]
else: # no text entered, all matches possible
self.matches = self.options[:]
# return match indexed by state
try:
return self.matches[state]
except IndexError:
return None
completer = MyCompleter(["hello", "hi", "how are you", "goodbye", "great"])
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
input = raw_input("Input: ")
print "You entered", input
这将导致以下行为(<TAB>表示按下的tab键):
Input: <TAB><TAB>
goodbye great hello hi how are you
Input: h<TAB><TAB>
hello hi how are you
Input: ho<TAB>ow are you
在最后一行(HOTAB输入)中,只有一个可能的匹配,整个“你好”语句将自动完成。
有关readline的详细信息,请查看链接的文章。
"And better yet would be if it would complete words not only from the beginning ... completion from arbitrary part of the string."
这可以通过简单地修改completer函数中的匹配条件来实现,即从:
self.matches = [s for s in self.options
if s and s.startswith(text)]
类似于:
self.matches = [s for s in self.options
if text in s]
这将给您以下行为:
Input: <TAB><TAB>
goodbye great hello hi how are you
Input: o<TAB><TAB>
goodbye hello how are you
我想你需要用户按下一个键。
您可以使用以下方法(无需按enter键)实现它:
然后,如果这个键是tab键(例如,这是您需要实现的),那么向用户显示所有的可能性。如果这是其他的键,请打印在标准输出上。
哦,当然,只要用户点击enter,就需要在一段时间内循环getkey()。你还可以得到一个方法,比如raw_input,当你点击一个标签时,它将得到整个单词的符号,或者显示所有的可能性。
至少你可以从这个开始。如果你有其他的问题,就写下来。
编辑1:
get-word方法可以如下所示:
我现在遇到的问题是如何显示一个符号,您刚刚输入时没有任何回车和空格,这是
print a
和print a,
所做的。(我知道这并不完全是您所要求的,但是)如果您对选项卡上出现的自动完成/建议(如许多shell中所使用的那样)感到满意,那么您可以使用readline模块快速启动并运行。
下面是一个基于Doug Hellmann's PyMOTW writeup on readline的快速示例。
这将导致以下行为(
<TAB>
表示按下的tab键):在最后一行(HOTAB输入)中,只有一个可能的匹配,整个“你好”语句将自动完成。
有关
readline
的详细信息,请查看链接的文章。这可以通过简单地修改completer函数中的匹配条件来实现,即从:
类似于:
这将给您以下行为:
更新:使用历史缓冲区(如注释中所述)
创建用于滚动/搜索的伪菜单的一个简单方法是将关键字加载到历史缓冲区中。然后,您将能够使用向上/向下箭头键滚动条目,并使用Ctrl+R执行反向搜索。
要尝试此操作,请进行以下更改:
运行脚本时,请尝试键入Ctrl+r,然后键入a。这将返回包含“a”的第一个匹配项。再次输入Ctrl+r进行下一个匹配。要选择条目,请按回车键。
也可以尝试使用向上/向下键滚动关键字。
要在Python shell中启用自动完成功能,请键入以下内容:
(感谢http://blog.e-shell.org/221)
相关问题 更多 >
编程相关推荐