在 urwid 中创建后退按钮

Creating a back button in urwid

我很难理解如何在 urwid 中加入返回到上一个 'screen' 的函数。

我复制了一些代码并对其进行了一些修改以执行我想要的操作,尽管如果我可以添加后退按钮或后退键绑定我会很高兴,但添加 button/binding 不是这个问题,我知道该怎么做,它会回到我正在寻求帮助的上一个菜单/'screen'

阅读文档

def TUI_torrents_list(torrents):
    body = [urwid.Text("InstantTorrent", align='center'), urwid.Divider()]
    for torrent in torrents:
        button = urwid.Button(torrent['title'])
        urwid.connect_signal(button, 'click', TUI_torrent_chosen, torrent)
        body.append(urwid.AttrMap(button, None, focus_map='reversed'))
    return urwid.ListBox(urwid.SimpleFocusListWalker(body))

def TUI_torrent_chosen(button, torrent):
    response = [urwid.Text(
        [
            'Title: {}\n\n'.format(torrent['title']),
            'Seeders: {}\n'.format(torrent['seeders']),
            'Leechers: {}\n'.format(torrent['leechers']),
            'Upload Date: {}\n'.format(torrent['date']),
            'Size: {}\n'.format(torrent['size']),
            'Source: {}\n'.format(torrent['source'])
        ]
    ), urwid.Divider()]
    # TODO: How can I incorperate more buttons in this TUI?
    # Download -> Opens the magnet URI using: open_torrent(mgnt_uri)
    # Back -> Returns to TUI_torrents_list
    copy = urwid.Button('Copy Magnet URI to clipboard')
    urwid.connect_signal(copy, 'click', copy_magnet_uri, torrent) # works

    download = urwid.Button('Download Torrent')
    urwid.connect_signal(download, 'click', open_torrent, torrent['mgnt_uri'])

    # back = urwid.Button('Back')
    # urwid.connect_signal(back, 'click',

    quit = urwid.Button('Quit')
    urwid.connect_signal(quit, 'click', TUI_exit_program)
    buttons = [copy, download, quit]
    for button in buttons:
        response.append(urwid.AttrMap(button, None, focus_map='reversed'))
    main.original_widget = urwid.Filler(urwid.Pile(response))


def TUI_exit_program(button):
    raise urwid.ExitMainLoop()

def TUI_exit_on_q(key):
    if key in ('q', 'Q'):
        raise urwid.ExitMainLoop()

def copy_magnet_uri(button, torrent):
    pyperclip.copy(torrent['mgnt_uri'])


if __name__ == '__main__':
    args = parse_args()
    torrents = []
    if args.query is None:
        # TODO: Replace this with erwid
        args.query = input("Enter your search query\n>_ ").strip()
    torrents += thepiratebay(args.query, args.proxy)
    torrents = sort_torrents(torrents, key='seeders')
    # output(torrents)
    main = urwid.Padding(TUI_torrents_list(torrents), left=2, right=2)
    top = urwid.Overlay(main, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
                        align='center', width=('relative', 80),
                        valign='middle', height=('relative', 80),
                        min_width=20, min_height=9)
    urwid.MainLoop(top, palette=[('reversed', 'standout', '')], unhandled_input=TUI_exit_on_q).run()

使用以下代码解决了这个问题并添加了一个调用此函数的按钮。

这个问题解决起来简单得令人尴尬

def TUI_back_to_torrents_list(button):
    main.original_widget = urwid.Padding(TUI_torrents_list(torrents), left=2, right=2