我很难理解如何在urwid中加入一个返回到上一个“屏幕”的功能。
我复制了其中的一些代码,并对其进行了一些修改,以做我想做的事情,虽然我喜欢它,如果我可以添加一个后退按钮或后退绑定,添加按钮/绑定不是问题,我知道如何做,它返回到以前的菜单/‘屏幕’,我正在寻找帮助
阅读文档
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()发布于 2019-10-11 10:16:14
用下面的代码解决了这个问题,并添加了一个调用此函数的按钮。
这个问题解决起来简单得令人尴尬。
def TUI_back_to_torrents_list(button):
main.original_widget = urwid.Padding(TUI_torrents_list(torrents), left=2, right=2https://stackoverflow.com/questions/58316590
复制相似问题