def __init__(self, *contents, tabbar=None):
"""Create new Tabs widget
contents: Iterable of dictionaries or iterables that match the arguments
of the `insert` method
tabbar: TabBar instance that is used to display tab titles or any object
with a 'base_widget' attribute (e.g. AttrMap) that returns a
TabBar object
"""
if tabbar is None:
self._tabbar = TabBar()
elif not isinstance(tabbar, urwid.Widget):
raise ValueError('tabbar must be TabBar instance, not {}: {!r}'
.format(type(tabbar).__name__, tabbar))
else:
self._tabbar = tabbar
self._ids = []
self._contents = urwid.MonitoredFocusList()
for content in contents:
if not isinstance(content, collections.Mapping):
content = dict(zip(('title', 'widget', 'position', 'focus'),
content))
self.insert(**content)
python类Widget()的实例源码
def test1(self):
a = urwid.Text("")
b = urwid.Text("")
blah = urwid.TextCanvas()
blah.finalize(a, (10,1), False)
blah2 = urwid.TextCanvas()
blah2.finalize(a, (15,1), False)
bloo = urwid.TextCanvas()
bloo.finalize(b, (20,2), True)
urwid.CanvasCache.store(urwid.Widget, blah)
urwid.CanvasCache.store(urwid.Widget, blah2)
urwid.CanvasCache.store(urwid.Widget, bloo)
self.cct(a, (10,1), False, blah)
self.cct(a, (15,1), False, blah2)
self.cct(a, (15,1), True, None)
self.cct(a, (10,2), False, None)
self.cct(b, (20,2), True, bloo)
self.cct(b, (21,2), True, None)
urwid.CanvasCache.invalidate(a)
self.cct(a, (10,1), False, None)
self.cct(a, (15,1), False, None)
self.cct(b, (20,2), True, bloo)
def test_display(test, options):
"""
Compose the element that will display the test
Returns:
[urwid.Widget]:
"""
empty_line = (DIV, options('weight', 1))
title = (urwid.Text(
('text bold', test['title'][0].upper() + test['title'][1:])), options('weight', 1))
caption = (urwid.Text(
('text', test['caption'])), options('weight', 1))
severity = (urwid.Text([
('input', 'Severity: '),
('text', ['HIGH', 'Medium', 'Low'][test['severity']])
]), options('weight', 1))
result = (urwid.Text([
('input', 'Result: '),
(
['failed', 'passed', 'warning', 'info'][test['result']],
['? FAILED', '? PASSED', '! WARNING', '* OMITTED'][test['result']]
)
]), options('weight', 1))
if isinstance(test['message'], list):
message_string = test['message'][0] + \
test['extra_data'] + test['message'][1]
else:
message_string = test['message']
message = (urwid.Text(
('text', message_string)), options('weight', 1))
return [empty_line, title, empty_line, severity, caption, result, message]
def test_display(test, options):
"""
Compose the element that will display the test
Returns:
[urwid.Widget]:
"""
empty_line = (DIV, options('weight', 1))
title = (urwid.Text(
('text bold', test['title'][0].upper() + test['title'][1:])), options('weight', 1))
caption = (urwid.Text(
('text', test['caption'])), options('weight', 1))
severity = (urwid.Text([
('input', 'Severity: '),
('text', ['HIGH', 'Medium', 'Low'][test['severity']])
]), options('weight', 1))
result = (urwid.Text([
('input', 'Result: '),
(
['failed', 'passed', 'warning', 'info'][test['result']],
['? FAILED', '? PASSED', '! WARNING', '* OMITTED'][test['result']]
)
]), options('weight', 1))
if isinstance(test['message'], list):
message_string = test['message'][0] + \
test['extra_data'] + test['message'][1]
else:
message_string = test['message']
message = (urwid.Text(
('text', message_string)), options('weight', 1))
return [empty_line, title, empty_line, severity, caption, result, message]
def insert(self, title, widget=None, position=-1, focus=True):
"""Insert new tab
title: Any flow or fixed widget to use as the tab's title
widget: Widget to show when this tab is selected or None
position: Where to insert the new tab; int for list-like index or
'right'/'left' to insert next to focused tab
focus: True to focus the new tab, False otherwise
Return TabID of inserted tab
"""
curpos = self.focus_position
if position == 'right':
newpos = (curpos+1) if curpos is not None else 0
elif position == 'left':
newpos = max(curpos, 0) if curpos is not None else 0
elif isinstance(position, int):
if position < 0:
newpos = position + len(self._contents) + 1
else:
newpos = position
else:
raise ValueError('Invalid position: {!r}'.format(position))
# Insert new tab ID
this_id = _find_unused_id(self._ids)
self._ids.insert(newpos, this_id)
# Insert title
self._tabbar.base_widget.insert(newpos, title)
# Insert content
self._contents.insert(newpos, widget)
if focus:
self.focus_position = newpos
return this_id
def cct(self, widget, size, focus, expected):
got = urwid.CanvasCache.fetch(widget, urwid.Widget, size, focus)
assert expected==got, "got: %s expected: %s"%(got, expected)