首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在urwid中创建后退按钮

在urwid中创建后退按钮
EN

Stack Overflow用户
提问于 2019-10-10 14:19:25
回答 1查看 140关注 0票数 0

我很难理解如何在urwid中加入一个返回到上一个“屏幕”的功能。

我复制了其中的一些代码,并对其进行了一些修改,以做我想做的事情,虽然我喜欢它,如果我可以添加一个后退按钮或后退绑定,添加按钮/绑定不是问题,我知道如何做,它返回到以前的菜单/‘屏幕’,我正在寻找帮助

阅读文档

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-11 10:16:14

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

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

代码语言:javascript
复制
def TUI_back_to_torrents_list(button):
    main.original_widget = urwid.Padding(TUI_torrents_list(torrents), left=2, right=2
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58316590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档