def menuAV(title, avail_stream_both): ###menu displaying formats with both audio and video ######### 2nd loop
body = [urwid.Text(title), urwid.Divider()]
for c in avail_stream_both:
button = urwid.Button(str(c) + " ----->" + str(c.resolution) + "----->" + str((float(c.get_filesize())/1024)/1024))
urwid.connect_signal(button, 'click', chosen_URL, c)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
button = urwid.Button("Only Video/Audio Formats")
urwid.connect_signal(button, 'click', menuVAOnly)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
button = urwid.Button("EXIT")
urwid.connect_signal(button, 'click', exit_program)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
return urwid.ListBox(urwid.SimpleFocusListWalker(body))
##########################################################################333
python类AttrMap()的实例源码
def menuVAOnlyMenu(title, avail_stream_VideoO,avail_stream_audioO): ###menu displaying formats with only audio or video ## must handle cases with audio and video alone ## for 3rd loop
body = [urwid.Text(title), urwid.Divider()]
for x in avail_stream_VideoO:
button = urwid.Button(str(x).split('@',1)[0] + "---->" +x.resolution + "----->" + str((float(x.get_filesize())/1024)/1024))
urwid.connect_signal(button, 'click', chosen_URL, x)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
for x1 in avail_stream_audioO:
button = urwid.Button(str(x1))
urwid.connect_signal(button, 'click', chosen_URL, x1)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
button = urwid.Button("EXIT")
urwid.connect_signal(button, 'click', exit_program)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
return urwid.ListBox(urwid.SimpleFocusListWalker(body))
#################3333##################################################
def __init__(self, prompt_text=u"Input:", default_text=u""):
close_button = urwid.Button("OK")
prompt = urwid.Text(prompt_text)
edit_field = CustomEdit(caption=u'',
edit_text=default_text,
multiline=False,
align='left',
wrap='space',
allow_tab=False,
edit_pos=None,
layout=None,
mask=None)
prompt_wrap = urwid.AttrMap(prompt, 'header')
close_button_wrap = urwid.AttrMap(close_button, 'buttn', 'buttnf')
edit_field_wrap = urwid.AttrMap(edit_field, 'editcp')
urwid.connect_signal(close_button, 'click', edit_field.on_finish)
urwid.connect_signal(edit_field, 'done', self.on_close)
pile = urwid.Pile([prompt_wrap,
edit_field_wrap,
urwid.Padding(close_button_wrap, 'center', 6)])
fill = urwid.Filler(urwid.Padding(pile, 'center', left=1, right=1))
self.__super.__init__(urwid.AttrWrap(fill, 'popbg'))
def __init__(self, list_item, show_hits, match=None):
self.list_item = list_item
if match is not None and match is not '' and show_hits is True:
# highlight the matches
hits = re.split('({match})'.format(match=re.escape(match)), self.list_item)
parts = []
for part in hits:
if part == match:
parts.append(('pattern', part))
else:
parts.append(part)
text = urwid.AttrMap(
urwid.Text(parts),
'line',
{'pattern': 'pattern_focus', None: 'line_focus'}
)
else:
text = urwid.AttrMap(urwid.Text(self.list_item), 'line', 'line_focus')
urwid.WidgetWrap.__init__(self, text)
def __init__(
self, title, login,
command_caption='Command: (Tab to switch focus to upper '
'frame, where you can scroll text)\nType exit or quit '
'to close', max_size=1000):
self.header = urwid.Text(title)
self.model = urwid.SimpleListWalker([])
self.body = ListView(
self.model, lambda: self._update_focus(False), max_size=max_size)
self.input = Input(lambda: self._update_focus(True))
foot = urwid.Pile([
urwid.AttrMap(
urwid.Text(command_caption),
'reversed'),
urwid.AttrMap(self.input, 'normal')])
urwid.Frame.__init__(self,
urwid.AttrWrap(self.body, 'normal'),
urwid.AttrWrap(self.header, 'reversed'),
foot)
self.set_focus_path(['footer', 1])
self._focus = True
urwid.connect_signal(self.input,
'line_entered',
self.on_line_entered)
self._output_styles = [s[0] for s in self.PALLETE]
self.eloop = None
self.login = login
def __init__(self, content, header=None, footer=None):
wlist = []
if header:
wlist.append(header)
wlist.extend([DIV, pad(content)])
if footer:
wlist.extend([HR, DIV, pad(footer)])
wlist.append(DIV)
card = urwid.AttrMap(urwid.Pile(wlist), 'card')
urwid.WidgetWrap.__init__(self, card)
def __init__(self, text, vertical_padding=True):
content = [urwid.Padding(self.get_content(text), left=3, right=3)]
if vertical_padding:
content = [DIV] + content + [DIV]
lbox = urwid.LineBox(urwid.Pile(content))
self.__super.__init__(urwid.AttrMap(urwid.Pile(
[lbox]), 'image button', 'image button focus'))
def __init__(self, pic, text):
content = self.get_content(text)
lbox = urwid.LineBox(urwid.Pile([DIV, urwid.Padding(
urwid.Columns([(8, pic), content], 4), left=3, right=3), DIV]))
self.__super.__init__(urwid.AttrMap(urwid.Pile(
[lbox]), 'image button', 'image button focus'))
def __init__(self, label="", label_width=15, next_callback=False):
self.label, self.next_callback = label, next_callback
self.edit = urwid.Padding(urwid.Edit(), left=1, right=1)
label = urwid.LineBox(
urwid.Text(label),
tlcorner=' ',
tline=' ',
lline=' ',
trcorner=' ',
blcorner=' ',
rline=' ',
brcorner=' ',
bline=' ')
lbox = urwid.AttrMap(
urwid.LineBox(
self.edit,
tlcorner=' ',
tline=' ',
lline=' ',
trcorner=' ',
blcorner=' ',
rline=' ',
brcorner=' '),
'input',
'input focus')
cols = urwid.Columns([(label_width, label), lbox])
urwid.WidgetWrap.__init__(self, cols)
def get_footer(text, callback):
return urwid.AttrMap(
TextButton(
text,
align='left',
on_press=(callback)),
'button')
def setup_view(self):
placeholder = urwid.SolidFill()
self.loop = urwid.MainLoop(
placeholder, PALETTE, unhandled_input=self.key_handler)
self.loop.widget = urwid.AttrMap(placeholder, 'bg')
#self.loop.widget._command_map['tab'] = 'cursor down'
#self.loop.widget._command_map['shift tab'] = 'cursor up'
self.loop.screen.set_terminal_properties(colors=256)
self.cards.welcome()
def render(self, card):
div = urwid.Divider()
rdiv = urwid.AttrMap(div, 'header')
header = urwid.Filler(urwid.Pile(
[rdiv, rdiv, rdiv, rdiv, rdiv]), valign='top')
h1_text = urwid.Text(('h1', self.name))
h2_text = urwid.Text(('h2', 'v' + self.version), align='right')
hg_text = urwid.AttrMap(urwid.Padding(urwid.Columns(
[h1_text, h2_text]), left=2, right=2, align='center'), 'header')
body = urwid.Pile([hg_text, rdiv, card, div])
widget = urwid.Overlay(body, header, 'center', 76, 'top', 'pack', top=1)
self.loop.widget.original_widget = widget
def run(self):
"""This method needs to be called to actually start the display.
To switch to another view (another Frame widget), use the
`@widget.setter` property of Loop. To return to earlier view
(or close a pop-up), use `@widget.deleter`. To interact with the
body of current Frame, use `@frameBody` property.
"""
frame = self.MOTD_screen()
# Top-level widget finished initializing, so let's assign it.
self.loop.Widget = ur.AttrMap(frame, "bg", None)
self.loop.run()
def MOTD_screen(self):
"""MOTD display method - first screen shown."""
self.motd_flag = True
self.header = self.make_header()
mid = ur.Padding(ur.ListBox(
ur.SimpleFocusListWalker([
self.div, ur.Text([("red", "Welcome to "),
("yellow", "sshchan!\n==========="),
("red", "========\n"),
("green", "SERVER: "),
self.config.server_name,
("green", "\nMOTD:\n")], "center"),
self.div])), "center", ("relative", 60), self.width,
self.margin, self.margin)
# TODO: add currently online users to the footer.
self.footer = ur.AttrMap(ur.Text(
" " + self.config.server_name + " " + self.config.version +
" | Press H for help", align="center"), "reverse", None)
try:
with open(self.config.motd, 'r') as m:
buf = m.read()
except FileNotFoundError:
buf = "---sshchan! woo!---"
motd = ur.Text(buf, "center")
mid.original_widget.body.append(motd)
mid.original_widget.body.append(self.div)
return ur.Frame(mid, self.header, self.footer, focus_part="body")
def show_help(self):
"""Create and return Frame object containing help docs."""
# Flags are gr8 against bugs.
self.help_flag = True
help_header = ur.Text(("green", "SSHCHAN HELP"), "center")
pg1 = ur.Text(
[("bold", "sshchan "), "is a textboard environment designed \
to run on remote SSH servers with multiple anonymous users simultaneously \
browsing and posting."], "center")
pg2 = ur.Text(("bold", "Keybindings:"))
pg3 = ur.Text([("green", "TAB"),
(
None,
" - switch focus between main body and top bar")])
pg4 = ur.Text(
[("green", "H h"), (None, " - display this help dialog")])
pg5 = ur.Text(
[("green", "B b"), (None, " - view available boards")])
pg6 = ur.Text(
[("green", "ESC"),
(
None,
" - go back one screen (exits on MOTD screen)")])
back_btn = ur.AttrMap(
ur.Button("Back", self.button_press, "back"), "red", "reverse")
help_body = ur.Padding(ur.ListBox(ur.SimpleListWalker([
self.div, help_header, self.div, pg1, self.div, pg2,
pg3, pg4, pg5, pg6, self.div, back_btn])),
"center", self.width, 0, self.margin, self.margin)
return ur.AttrMap(ur.Frame(help_body, self.header, self.footer,
"body"), "bg")
def print_thread(self, button, thread):
thr_no = self.board.thread_exists(int(thread))
thr_body = self.board.get_index()[thr_no]
replies = ur.SimpleFocusListWalker([])
subject = ("reverse_red", thr_body[1])
if subject[1] == "":
subject = (None, "")
op = self.parse_post(thr_body[2])
op_info = ur.Text([("reverse_green", op["name"]),
" " + op["stamp"] + " ", subject])
op_btn = CleanButton(
"No. " + op["id"], self.reply_box, op["id"])
op_widget = ur.AttrMap(ur.Columns(
[("pack", op_info), ("pack", op_btn)], 1), "reverse")
replies.extend([op_widget, op["text"], self.parent.div])
if len(thr_body) > 3:
for postno in range(3, len(thr_body)):
reply = self.parse_post(thr_body[postno])
reply_info = ur.Text(
[("green", reply["name"]), " " + reply["stamp"]])
reply_btn = CleanButton(
"No. " + reply["id"], self.reply_box, reply["id"])
reply_widget = ur.Columns(
[("pack", reply_info), ("pack", reply_btn)],
1)
replies.extend([reply_widget, reply["text"], self.parent.div])
contents = ur.ListBox(replies)
contents.set_focus(0)
self.loop.Widget = ur.Frame(
contents, self.parent.header, self.parent.footer, "body")
def __init__(self, test_id, text, escape_method):
self.escape_method = escape_method
lines = text.split('\n')
list_items = [
urwid.AttrMap(urwid.Text(line), None, focus_map='reversed') for line in lines
]
super(TestResultWindow, self).__init__(
urwid.ListBox(
urwid.SimpleFocusListWalker(list_items)
),
title=test_id
)
def get_list_item(self, test_id, position):
test_data = self.store.test_data[test_id]
test_data.update({
'widget': None,
'lw_widget': None,
'position': position,
'id': test_id,
})
test_line = TestLine(test_data)
test_data['widget'] = test_line
# logger.debug('widget set for %s: %s', test_id, test_line)
urwid.connect_signal(test_line, 'click', self.show_test_detail, test_id)
test_line_attr = urwid.AttrMap(test_line, None, focus_map='reversed')
test_data['lw_widget'] = test_line_attr
return test_line_attr
def redraw_tabs(self):
# this is done to ensure that the bottom bar is re-drawn after opening files
foot_col = urwid.Columns(self.display.tabs)
foot = urwid.AttrMap(foot_col, 'footer')
if self.display.layout:
self.display.top.contents['header'] = (foot, None)
else:
self.display.top.contents['footer'] = (foot, None)
def update_top_bar(self):
self.stext = ('header', ['SCUM ',
('key', 'ESC'), ' Help ',
('key', self.config['save']), ' Save ',
('key', self.config['open']), ' Open ',
('key', self.config['exit']), ' Exit' ])
self.tbar = urwid.Text(self.stext)
self.status = urwid.AttrMap(self.tbar, 'header')
self.top.contents['header'] = (self.status, None)