本文整理汇总了Python中urwid.ListBox.set_focus方法的典型用法代码示例。如果您正苦于以下问题:Python ListBox.set_focus方法的具体用法?Python ListBox.set_focus怎么用?Python ListBox.set_focus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urwid.ListBox
的用法示例。
在下文中一共展示了ListBox.set_focus方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AddCharmDialog
# 需要导入模块: from urwid import ListBox [as 别名]
# 或者: from urwid.ListBox import set_focus [as 别名]
class AddCharmDialog(Overlay):
""" Adding charm dialog """
def __init__(self, underlying, juju_state, destroy, command_runner=None):
import cloudinstall.charms
charm_modules = [import_module('cloudinstall.charms.' + mname)
for (_, mname, _) in
pkgutil.iter_modules(cloudinstall.charms.__path__)]
charm_classes = [m.__charm_class__ for m in charm_modules
if m.__charm_class__.allow_multi_units]
self.cr = command_runner
self.underlying = underlying
self.destroy = destroy
self.boxes = []
self.bgroup = []
first_index = 0
for i, charm_class in enumerate(charm_classes):
charm = charm_class(juju_state=juju_state)
if charm.name() and not first_index:
first_index = i
r = RadioButton(self.bgroup, charm.name())
r.text_label = charm.name()
self.boxes.append(r)
self.count_editor = IntEdit("Number of units to add: ", 1)
self.boxes.append(self.count_editor)
wrapped_boxes = _wrap_focus(self.boxes)
bs = [Button("Ok", self.yes), Button("Cancel", self.no)]
wrapped_buttons = _wrap_focus(bs)
self.buttons = Columns(wrapped_buttons)
self.items = ListBox(wrapped_boxes)
self.items.set_focus(first_index)
ba = BoxAdapter(self.items, height=len(wrapped_boxes))
self.lb = ListBox([ba, Text(""), self.buttons])
self.w = LineBox(self.lb, title="Add unit")
self.w = AttrMap(self.w, "dialog")
Overlay.__init__(self, self.w, self.underlying,
'center', 45, 'middle', len(wrapped_boxes) + 4)
def yes(self, button):
selected = [r for r in self.boxes if
r is not self.count_editor
and r.get_state()][0]
_charm_to_deploy = selected.label
n = self.count_editor.value()
log.info("Adding {n} units of {charm}".format(
n=n, charm=_charm_to_deploy))
self.cr.add_unit(_charm_to_deploy, count=int(n))
self.destroy()
def no(self, button):
self.destroy()
示例2: ChangeStateDialog
# 需要导入模块: from urwid import ListBox [as 别名]
# 或者: from urwid.ListBox import set_focus [as 别名]
class ChangeStateDialog(Overlay):
def __init__(self, underlying, juju_state, on_success, on_cancel):
import cloudinstall.charms
charm_modules = [import_module('cloudinstall.charms.' + mname)
for (_, mname, _) in
pkgutil.iter_modules(cloudinstall.charms.__path__)]
charm_classes = sorted([m.__charm_class__ for m in charm_modules],
key=attrgetter('deploy_priority'))
self.boxes = []
first_index = 0
for i, charm_class in enumerate(charm_classes):
charm = charm_class(juju_state=juju_state)
if charm.name() and not first_index:
first_index = i
r = CheckBox(charm.name())
r.text_label = charm.name()
self.boxes.append(r)
wrapped_boxes = _wrap_focus(self.boxes)
def ok(button):
selected = filter(lambda r: r.get_state(), self.boxes)
on_success([s.text_label for s in selected])
def cancel(button):
on_cancel()
bs = [Button("Ok", ok), Button("Cancel", cancel)]
wrapped_buttons = _wrap_focus(bs)
self.buttons = Columns(wrapped_buttons)
self.items = ListBox(wrapped_boxes)
self.items.set_focus(first_index)
ba = BoxAdapter(self.items, height=len(wrapped_boxes))
self.lb = ListBox([ba, self.count_editor, self.buttons])
root = LineBox(self.lb, title="Select new charm")
root = AttrMap(root, "dialog")
Overlay.__init__(self, root, underlying, 'center', 30, 'middle',
len(wrapped_boxes) + 4)
def keypress(self, size, key):
if key == 'tab':
if self.lb.get_focus()[0] == self.buttons:
self.keypress(size, 'page up')
else:
self.keypress(size, 'page down')
return Overlay.keypress(self, size, key)
示例3: _insert_charm_selections
# 需要导入模块: from urwid import ListBox [as 别名]
# 或者: from urwid.ListBox import set_focus [as 别名]
def _insert_charm_selections(self):
first_index = 0
bgroup = []
for i, charm_class in enumerate(self.charms):
charm = charm_class
if charm.name() and not first_index:
first_index = i
r = RadioButton(bgroup, charm.name())
r.text_label = charm.name()
self.boxes.append(r)
# Add input widget for specifying NumUnits
self.count_editor = IntEdit("Number of units to add: ", 1)
self.boxes.append(self.count_editor)
wrapped_boxes = self._wrap_focus(self.boxes)
items = ListBox(SimpleListWalker(wrapped_boxes))
items.set_focus(first_index)
return (len(self.boxes), BoxAdapter(items, len(self.boxes)))
示例4: SearchDialog
# 需要导入模块: from urwid import ListBox [as 别名]
# 或者: from urwid.ListBox import set_focus [as 别名]
class SearchDialog(Dialog):#{{{
title = "SearchDialog"
subtitle = "GenericWindow"
_items = []
def __init__(self, **kwargs):#{{{
self.search_box = SearchBox()
self.items_count = Text("", align='right')
self.search_items = SimpleListWalker(self._null_list_item())
connect_signal(self.search_items, "modified", self._update_items_count)
self.search_list = ListBox(self.search_items)
connect_signal(self.search_box, "edit-done", self.on_edit_done)
connect_signal(self.search_box, "edit-cancel", lambda w: self.quit())
connect_signal(self.search_box, "change", lambda sb, term: self.set_search_term(term))
self._constr = self.get_item_constructor()
self.multiple_selection = kwargs.pop('multiple_selection', False)
self._selected_items = OrderedSet([])
opts = {
'height': kwargs.get('height', None),
'width': kwargs.get('width', ('relative', 90)),
'title': kwargs.get('title', self.title),
'subtitle': kwargs.get('subtitle', self.subtitle),
'compact_header': kwargs.get('compact_header', True),
}
kwargs.update(opts)
self.pile = Pile([
('fixed', 15, AttrMap(self.search_list, 'dialog.search.item')),
Columns([
AttrMap(self.search_box, 'dialog.search.input'),
('fixed', 1, AttrMap(Divider(), 'dialog.search.input')),
('fixed', 4, AttrMap(self.items_count, 'dialog.search.input')),
]),
], focus_item=0)
self.__super.__init__(self.pile, **kwargs)
self.attr_style = "dialog.search"
self.title_attr_style = "dialog.search.title"
self.subtitle_attr_style = "dialog.search.subtitle"
#}}}
def keypress(self, key):#{{{
if key == 'insert' and self.multiple_selection:
if self.pile.get_focus().original_widget is self.search_list:
wid, pos = self.search_list.get_focus()
else:
pos = 0
current_item = self.search_items[pos]
article = self.get_data_for(pos)
current_item.original_widget.selected = not current_item.original_widget.selected
if current_item.original_widget.selected:
current_item.attr_map = {None: 'dialog.search.item.selected'}
current_item.focus_map = {None: 'dialog.search.item.focus.selected'}
self._selected_items.add(article)
else:
current_item.attr_map = {None: 'dialog.search.item'}
current_item.focus_map = {None: 'dialog.search.item.focus'}
self._selected_items.discard(article)
self.search_list.set_focus(pos+1)
self._update_items_count()
#}}}
def on_edit_done(self, widget, text):#{{{
result = []
if self.pile.get_focus().original_widget is self.search_list:
wid, pos = self.search_list.get_focus()
else:
pos = 0
if self.multiple_selection:
result = list(self._selected_items)
if len(result) < 1:
if self.get_data_for(pos):
result = [self.get_data_for(pos)]
self.dialog_result = result
self.quit()
#}}}
def set_search_term(self, term):#{{{
self._clear_search_items()
query = self.get_query(term)
if query is not None:
self._items = tuple(query[:150])
if len(self._items) > 0:
l_items = map(self._constr, self._items)
for i in l_items:
i.set_search_box(self.search_box)
self.search_items.extend([AttrMap(i, 'dialog.search.item',\
'dialog.search.item.focus') for i in l_items])
if self.multiple_selection:
for a in (self._selected_items & set(self._items)):
idx = self._items.index(a)
self.search_items[idx].attr_map = {None: 'dialog.search.item.selected'}
self.search_items[idx].focus_map = {None: 'dialog.search.item.focus.selected'}
self.search_items[idx].original_widget.selected = True
return
#.........这里部分代码省略.........
示例5: TreeBox
# 需要导入模块: from urwid import ListBox [as 别名]
# 或者: from urwid.ListBox import set_focus [as 别名]
class TreeBox(WidgetWrap):
"""
A widget representing something in a nested tree display.
This is essentially a ListBox with the ability to move the focus based on
directions in the Tree.
TreeBox interprets `left/right` as well as page `up/down` to move the focus
to parent/first child and next/previous sibling respectively. All other
keys are passed to the underlying ListBox.
"""
_selectable = True
def __init__(self, walker, **kwargs):
"""
:param walker: tree of widgets to be displayed.
In case we are given a raw `TreeWalker`, it will be used though
`TreeListWalker` which means no decoration.
:type walker: TreeWalker or TreeListWalker
"""
if not isinstance(walker, TreeListWalker):
walker = TreeListWalker(walker)
self._walker = walker
self._outer_list = ListBox(walker)
self.__super.__init__(self._outer_list)
# Widget API
def get_focus(self):
return self._outer_list.get_focus()
def keypress(self, size, key):
key = self._outer_list.keypress(size, key)
if key in ['left', 'right', '[', ']', '-', '+', 'C', 'E']:
if key == 'left':
self.focus_parent()
elif key == 'right':
self.focus_first_child()
elif key == '[':
self.focus_prev_sibling()
elif key == ']':
self.focus_next_sibling()
if isinstance(self._walker, CollapseMixin):
if key == '-':
w, focuspos = self._walker.get_focus()
self._walker.collapse(focuspos)
elif key == '+':
w, focuspos = self._walker.get_focus()
self._walker.expand(focuspos)
elif key == 'C':
self._walker.collapse_all()
elif key == 'E':
self._walker.expand_all()
# This is a hack around ListBox misbehaving:
# it seems impossible to set the focus without calling keypress as
# otherwise the change becomes visible only after the next render()
return self._outer_list.keypress(size, None)
else:
return self._outer_list.keypress(size, key)
# Tree based focus movement
def focus_parent(self):
w, focuspos = self._walker.get_focus()
parent = self._walker.parent_position(focuspos)
if parent is not None:
self._outer_list.set_focus(parent)
def focus_first_child(self):
w, focuspos = self._walker.get_focus()
child = self._walker.first_child_position(focuspos)
if child is not None:
self._outer_list.set_focus(child)
def focus_next_sibling(self):
w, focuspos = self._walker.get_focus()
sib = self._walker.next_sibling_position(focuspos)
if sib is not None:
self._outer_list.set_focus(sib)
def focus_prev_sibling(self):
w, focuspos = self._walker.get_focus()
sib = self._walker.prev_sibling_position(focuspos)
if sib is not None:
self._outer_list.set_focus(sib)
示例6: TreeBox
# 需要导入模块: from urwid import ListBox [as 别名]
# 或者: from urwid.ListBox import set_focus [as 别名]
class TreeBox(WidgetWrap):
"""
A widget that displays a given :class:`Tree`.
This is essentially a :class:`ListBox` with the ability to move the focus
based on directions in the Tree and to collapse/expand subtrees if
possible.
TreeBox interprets `left/right` as well as `page up/`page down` to move the
focus to parent/first child and next/previous sibling respectively. All
other keys are passed to the underlying ListBox.
"""
def __init__(self, tree, focus=None):
"""
:param tree: tree of widgets to be displayed.
:type tree: Tree
:param focus: initially focussed position
"""
self._tree = tree
self._walker = TreeListWalker(tree)
self._outer_list = ListBox(self._walker)
if focus is not None:
self._outer_list.set_focus(focus)
self.__super.__init__(self._outer_list)
# Widget API
def get_focus(self):
return self._outer_list.get_focus()
def set_focus(self, pos):
return self._outer_list.set_focus(pos)
def refresh(self):
self._walker.clear_cache()
signals.emit_signal(self._walker, "modified")
def keypress(self, size, key):
key = self._outer_list.keypress(size, key)
if key in ["left", "right", "[", "]", "-", "+", "C", "E"]:
if key == "left":
self.focus_parent()
elif key == "right":
self.focus_first_child()
elif key == "[":
self.focus_prev_sibling()
elif key == "]":
self.focus_next_sibling()
elif key == "-":
self.collapse_focussed()
elif key == "+":
self.expand_focussed()
elif key == "C":
self.collapse_all()
elif key == "E":
self.expand_all()
# This is a hack around ListBox misbehaving:
# it seems impossible to set the focus without calling keypress as
# otherwise the change becomes visible only after the next render()
return self._outer_list.keypress(size, None)
else:
return self._outer_list.keypress(size, key)
# Collapse operations
def collapse_focussed(self):
"""
Collapse currently focussed position; works only if the underlying
tree allows it.
"""
if implementsCollapseAPI(self._tree):
w, focuspos = self.get_focus()
self._tree.collapse(focuspos)
self._walker.clear_cache()
self.refresh()
def expand_focussed(self):
"""
Expand currently focussed position; works only if the underlying
tree allows it.
"""
if implementsCollapseAPI(self._tree):
w, focuspos = self.get_focus()
self._tree.expand(focuspos)
self._walker.clear_cache()
self.refresh()
def collapse_all(self):
"""
Collapse all positions; works only if the underlying tree allows it.
"""
if implementsCollapseAPI(self._tree):
self._tree.collapse_all()
self.set_focus(self._tree.root)
self._walker.clear_cache()
self.refresh()
def expand_all(self):
"""
Expand all positions; works only if the underlying tree allows it.
"""
if implementsCollapseAPI(self._tree):
#.........这里部分代码省略.........
示例7: CursesGUI
# 需要导入模块: from urwid import ListBox [as 别名]
# 或者: from urwid.ListBox import set_focus [as 别名]
class CursesGUI(object):
def __init__(self, choice_callback=None,
command_callback=None,
help_callback=None):
self.palette = [
('brick', 'light red', 'black'),
('rubble', 'yellow', 'black'),
('wood', 'light green', 'black'),
('concrete', 'white', 'black'),
('stone', 'light cyan', 'black'),
('marble', 'light magenta', 'black'),
('jack', 'dark gray', 'white'),
('msg_info', 'white', 'black'),
('msg_err', 'light red', 'black'),
('msg_debug', 'light green', 'black'),
]
self.choice_callback = choice_callback
self.command_callback = command_callback
self.help_callback = help_callback
self.screen = None
self.loop = None
self.called_loop_stop = False
self.reactor_stop_fired = False
self.quit_flag = False
self.edit_msg = "Make selection ('q' to quit): "
self.roll_list = SimpleListWalker([])
self.game_log_list = SimpleListWalker([])
self.choices_list = SimpleListWalker([])
self.state_text = SimpleListWalker([Text('Connecting...')])
self.edit_widget = Edit(self.edit_msg)
self.roll = ListBox(self.roll_list)
self.game_log = ListBox(self.game_log_list)
self.choices = ListBox(self.choices_list)
self.state = ListBox(self.state_text)
self.left_frame = Pile([
LineBox(self.state),
(13, LineBox(self.choices)),
])
self.right_frame = Pile([
LineBox(self.game_log),
LineBox(self.roll)
])
self.state.set_focus(len(self.state_text)-1)
self.columns = Columns([('weight', 0.75, self.left_frame),
('weight', 0.25, self.right_frame)
])
self.frame_widget = Frame(footer=self.edit_widget,
body=self.columns,
focus_part='footer')
self.exc_info = None
def register_loggers(self):
"""Gets the global loggers and sets up log handlers.
"""
self.game_logger_handler = RollLogHandler(self._roll_write)
self.logger_handler = RollLogHandler(self._roll_write)
self.game_logger = logging.getLogger('gtr.game')
self.logger = logging.getLogger('gtr')
self.logger.addHandler(self.logger_handler)
self.game_logger.addHandler(self.game_logger_handler)
#self.set_log_level(logging.INFO)
def unregister_loggers(self):
self.game_logger.removeHandler(self.game_logger_handler)
self.logger.removeHandler(self.logger_handler)
def fail_safely(f):
"""Wraps functions in this class to catch arbitrary exceptions,
shut down the event loop and reset the terminal to a normal state.
It then re-raises the exception.
"""
@wraps(f)
def wrapper(self, *args, **kwargs):
retval = None
try:
retval = f(self, *args, **kwargs)
except urwid.ExitMainLoop:
from twisted.internet import reactor
if not self.reactor_stop_fired and reactor.running:
# Make sure to call reactor.stop once
reactor.stop()
#.........这里部分代码省略.........