Pulling content from a selected item in a ListBox (urwid)

情到浓时终转凉″ 提交于 2021-01-28 12:20:24

问题


I'm making a ListBox in urwid where each item is a URL pulled from a list of URLs. When an item is selected and ENTER is clicked, I'd like to pull the URL and open it in the browser. Here's my code so far:

class SelectableText(urwid.Text):
    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

def handle_input(input):
    if input == "": # Open link
        focus_widget, idx = content_container.get_focus()
        # TODO: Open link from focus_widget
    elif input in ('q', 'Q'): # Quit
        raise urwid.ExitMainLoop()

palette = [
  ('menu', 'black', 'dark cyan', 'standout'),
  ('reveal focus', 'black', 'dark cyan', 'standout')]
menu = urwid.Text([
    u'\n',
    ('menu', u' ENTER '), ('light gray', u" Open link "),
    ('menu', u' Q '), ('light gray', u" Quit"),
])

list_of_urls = ["http://thisisalink.com/", ...]
items = list(map(lambda url: urwid.AttrMap(SelectableText(url), None, "reveal focus"), list_of_urls))
content = urwid.SimpleListWalker(items)
content_container = urwid.ListBox(content)

layout = urwid.Frame(body=content_container, footer=menu)

main_loop = urwid.MainLoop(layout, palette, unhandled_input=handle_input)
main_loop.run()

The issue is in handle_input(input). How do I pull the actual URL from the results of content_container.get_focus()?


回答1:


In focus_widget you have the AttrMap object wrapping the text widget.

You can get the text content from a urwid.Text widget with the .text property, and you can get the widget wrapped by an urwid.AttrMap with the .base_widget attribute.

Note also that you need to check if input is equal to enter, to check for the Enter key

def handle_input(input):
    if input == "enter": # Open link
        focus_widget, idx = content_container.get_focus()
        url = focus_widget.base_widget.text
        import webbrowser
        webbrowser.open(url)
    elif input in ('q', 'Q'): # Quit
        raise urwid.ExitMainLoop()


来源:https://stackoverflow.com/questions/49499551/pulling-content-from-a-selected-item-in-a-listbox-urwid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!