我找不到合适的方法来更新URWID中的SimpleWalkerList内容。下面是我试图根据用户输入生成列表的代码的简化示例:
import urwid
palette = [('header', 'white', 'black'),
('reveal focus', 'black', 'dark cyan', 'standout')]
items = [urwid.Text("foo"),
urwid.Text("bar"),
urwid.Text("baz")]
content = urwid.SimpleListWalker([
urwid.AttrMap(w, None, 'reveal focus') for w in items])
listbox = urwid.ListBox(content)
show_key = urwid.Text("Press any key", wrap='clip')
head = urwid.AttrMap(show_key, 'header')
top = urwid.Frame(listbox, head)
def show_all_input(input, raw):
show_key.set_text("Pressed: " + " ".join([
unicode(i) for i in input]))
return input
def exit_on_cr(input):
if input in ('q', 'Q'):
raise urwid.ExitMainLoop()
elif input == 'up':
focus_widget, idx = listbox.get_focus()
if idx > 0:
idx = idx-1
listbox.set_focus(idx)
elif input == 'down':
focus_widget, idx = listbox.get_focus()
idx = idx+1
listbox.set_focus(idx)
elif input == 'enter':
pass
#
#how here can I change value of the items list and display the ne value????
#
def out(s):
show_key.set_text(str(s))
loop = urwid.MainLoop(top, palette,
input_filter=show_all_input, unhandled_input=exit_on_cr)
loop.run()预期的结果将是将值从“foo”更改为“oof”(非常简单的字符串操作)。不管我用的是什么方式,都不允许我操纵这些价值观。我需要制动回路并从头开始重新绘制整个屏幕吗?
提前感谢!
发布于 2018-08-09 01:39:10
正如SimpleListWalker文档中所解释的那样
目录-要复制到此对象的列表 对此对象所做的更改(将其视为列表时)将被自动检测,并将导致使用此列表遍历器的ListBox对象被更新。
因此,您所要做的就是修改列表中的元素:
elif input in 'rR':
_, idx = listbox.get_focus()
am = content[idx].original_widget
am.set_text(am.text[::-1])即使列表中有不可变的值,也可以用新的对象替换它们:
elif input in 'rR':
_, idx = listbox.get_focus()
w = urwid.Text(content[idx].original_widget.text[::-1])
content[idx] = urwid.AttrMap(w, None, 'reveal focus')但是,由于您没有任何不可变的对象妨碍您,所以这是不必要的;第一个版本可以正常工作。
无论哪种方式,如果您按r,您所指向的任何文本都会反转,就像foo到oof一样。(当然,如果您使用的是任何标记,您需要做一些比这更小心的事情,但您没有。)
https://stackoverflow.com/questions/51757605
复制相似问题