python - urwid 简单列表框示例
我正在考虑 urwid 来开发一个控制台应用程序。作为起点,我正在寻找一个简单的列表框示例,该列表框显示可以使用向上/向下键滚动的几行。
任何人都可以提供一个简单的示例来说明如何做到这一点或向我指出一个链接吗?我在 urwid 网站上看到了示例,但我正在寻找更基本的东西。
编辑
@Adam:我在网上找到了这个例子。我对注释掉的部分感到困难,因为我不熟悉 API 并且我是 python 新手。
编辑2 我弄清楚了并更新了示例。 还有一个问题 - 我可以直接从 ListBox 获取项目计数吗?
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)
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()
I'm looking at urwid to develop a console app. As a starting point I'm looking for a simple example of a listbox that displays a few rows that can be scrolled using up/down keys.
Can anyone provide a simple example of how this can be done or point me to a link? I saw the examples on urwid site but I'm looking for something more basic.
Edit
@Adam: I found this example online. I'm having difficulties with the commented out part as I'm not familiar with the API and I'm a python newbie.
Edit2
I figured it out and updated the example.
One more question - can I get the item count directly from the ListBox?
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)
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
列表框在它们的 body 属性中共享它们的 SimpleListWalker ,幸运的是,它正确地实现了 len
len(listbox.body)
进一步的证据:
listboxes share their SimpleListWalker inside their body attribute which fortunately implements len correctly
len(listbox.body)
Further evidence :
我可以直接从列表框中获取项目计数吗?
这将给出许多被选择的项目
这将给出所选的索引和文本
can I get the item count directly from the ListBox?
This will give many items being selected
This will give the selected index and text