本文整理汇总了Python中urwid.Edit.keypress方法的典型用法代码示例。如果您正苦于以下问题:Python Edit.keypress方法的具体用法?Python Edit.keypress怎么用?Python Edit.keypress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urwid.Edit
的用法示例。
在下文中一共展示了Edit.keypress方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TextEditor
# 需要导入模块: from urwid import Edit [as 别名]
# 或者: from urwid.Edit import keypress [as 别名]
class TextEditor(WidgetWrap):
"""Editor for creating arbitrary text."""
__metaclass__ = signals.MetaSignals
signals = ['done']
def __init__(self,
prompt,
content,
done_signal_handler):
if content:
content += ' '
self.editor = Edit(u'%s (twice enter key to validate or esc) \n>> ' % prompt, content)
widgets = [self.editor]
w = AttrMap(Columns(widgets), 'editor')
connect_signal(self, 'done', done_signal_handler)
self.__super.__init__(w)
def keypress(self, size, key):
if key == 'enter' and self.last_key == 'enter':
self.emit_done_signal(self.editor.get_edit_text())
return
elif key == 'esc':
self.emit_done_signal()
return
self.last_key = key
size = size,
self.editor.keypress(size, key)
def emit_done_signal(self, content=None):
emit_signal(self, 'done', content)
示例2: BaseEditor
# 需要导入模块: from urwid import Edit [as 别名]
# 或者: from urwid.Edit import keypress [as 别名]
class BaseEditor(WidgetWrap):
"""Base class for editors."""
__metaclass__ = signals.MetaSignals
signals = ["done"]
def __init__(self, prompt, content, done_signal_handler, cursor_position=None):
"""
Initializes editor, connects 'done' signal.
When pressing 'enter' twice the `submit` method is called, which by
default calls `emit_done_signal` with the text that has been
introduced.
When pressing 'esc' the `cancel` method is called, which by default
calls `emit_done_signal` with no arguments.
The subclasses must call the `_wrap` method with the editor widgets
and `BaseEditor` will wrap it in a `urwid.Colums` widget, calling to
`urwid.WidgetWrap.__init__` with the wrapped widget.
"""
caption = _(u"%s (twice enter key to validate or esc) \n>> ") % prompt
if content:
content += " "
self.content = content
self.editor = Edit(caption=caption, edit_text=content, edit_pos=cursor_position)
self.last_key = None
connect_signal(self, "done", done_signal_handler)
def _wrap(self, widgets):
widgets = widgets if isinstance(widgets, list) else [widgets]
composed_widget = Columns(widgets)
widget = AttrMap(LineBox(composed_widget), "editor")
WidgetWrap.__init__(self, widget)
def keypress(self, size, key):
if key == "enter" and self.last_key == "enter":
self.submit()
return
elif key == "esc":
self.cancel()
return
self.last_key = key
size = (size,)
self.editor.keypress(size, key)
def submit(self):
self.emit_done_signal(self.editor.get_edit_text())
def cancel(self):
self.emit_done_signal()
def emit_done_signal(self, content=None):
emit_signal(self, "done", content)
示例3: TweetEditor
# 需要导入模块: from urwid import Edit [as 别名]
# 或者: from urwid.Edit import keypress [as 别名]
class TweetEditor(WidgetWrap):
"""Editor for creating tweets."""
__metaclass__ = signals.MetaSignals
signals = ['done']
def __init__(self,
prompt,
content,
done_signal_handler):
if content:
content += ' '
self.editor = Edit(u'%s (twice enter key to validate or esc) \n>> ' % prompt, content)
self.counter = len(content)
self.counter_widget = Text(str(self.counter))
widgets = [('fixed', 4, self.counter_widget), self.editor]
w = AttrMap(Columns(widgets), 'editor')
connect_signal(self, 'done', done_signal_handler)
connect_signal(self.editor, 'change', self.update_counter)
self.__super.__init__(w)
def update_counter(self, edit, new_edit_text):
self.counter = len(new_edit_text)
self.counter_widget.set_text(str(self.counter))
def keypress(self, size, key):
if key == 'enter' and self.last_key == 'enter':
if self.counter > TWEET_MAX_CHARS:
return
else:
self.emit_done_signal(self.editor.get_edit_text())
elif key == 'esc':
self.emit_done_signal()
return
self.last_key = key
size = size,
self.editor.keypress(size, key)
def emit_done_signal(self, content=None):
emit_signal(self, 'done', content)
示例4: keypress
# 需要导入模块: from urwid import Edit [as 别名]
# 或者: from urwid.Edit import keypress [as 别名]
def keypress(self, size, key):
if key == 'enter':
self._top[1]()
self._done(self.get_edit_text())
elif key == 'esc':
self._top[1]()
elif key == 'tab' and self._tab:
text = self.get_edit_text()[:self.edit_pos]
text_len = len(text)
match = None
for entry in self._tab(text):
if entry.startswith(text):
entry = entry[text_len:]
if match is None:
match = entry
else:
while not entry.startswith(match):
match = match[:-1]
if match:
self.insert_text(match)
else:
return Edit.keypress(self, size, key)
示例5: BaseEditor
# 需要导入模块: from urwid import Edit [as 别名]
# 或者: from urwid.Edit import keypress [as 别名]
class BaseEditor(with_metaclass(signals.MetaSignals, WidgetWrap)):
"""Base class for editors."""
signals = ['done']
def __init__(self,
prompt,
content,
done_signal_handler,
cursor_position=None):
"""
Initializes editor, connects 'done' signal.
When pressing 'enter' twice the `submit` method is called, which by
default calls `emit_done_signal` with the text that has been
introduced.
When pressing 'esc' the `cancel` method is called, which by default
calls `emit_done_signal` with no arguments.
The subclasses must call the `_wrap` method with the editor widgets
and `BaseEditor` will wrap it in a `urwid.Colums` widget, calling to
`urwid.WidgetWrap.__init__` with the wrapped widget.
"""
caption = _(u'{0} (Enter key twice to validate, '
u'Esc or Ctrl-C to cancel) \n>> ').format(prompt)
if content:
content += ' '
self.content = content
self.editor = Edit(caption=caption,
edit_text=content,
edit_pos=cursor_position)
self.last_key = None
connect_signal(self, 'done', done_signal_handler)
def _wrap(self, widgets):
widgets = widgets if isinstance(widgets, list) else [widgets]
composed_widget = Columns(widgets)
widget = AttrMap(LineBox(composed_widget), 'editor')
super(BaseEditor, self).__init__(widget)
def keypress(self, size, key):
if key == 'enter' and self.last_key == 'enter':
self.submit()
return
elif key == 'esc':
self.cancel()
return
self.last_key = key
size = size,
self.editor.keypress(size, key)
def submit(self):
self.emit_done_signal(self.editor.get_edit_text())
def cancel(self):
self.emit_done_signal()
def emit_done_signal(self, content=None):
emit_signal(self, 'done', content)