python - urwid 简单列表框示例

发布于 2024-12-12 19:05:06 字数 1457 浏览 1 评论 0原文

我正在考虑 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2024-12-19 19:05:06

列表框在它们的 body 属性中共享它们的 SimpleListWalker ,幸运的是,它正确地实现了 len

len(listbox.body)

进一步的证据:

(Pdb) listbox.body
SimpleListWalker([<AttrMap flow widget <Text flow widget 'Chemma!'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'Another text widget!'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'What is your name'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'Boy ?'> attr_map={None: None} focus_map={None: 'line'}>])
(Pdb) len(listbox.body)
7
(Pdb) 

listboxes share their SimpleListWalker inside their body attribute which fortunately implements len correctly

len(listbox.body)

Further evidence :

(Pdb) listbox.body
SimpleListWalker([<AttrMap flow widget <Text flow widget 'Chemma!'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'Another text widget!'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'What is your name'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'Boy ?'> attr_map={None: None} focus_map={None: 'line'}>])
(Pdb) len(listbox.body)
7
(Pdb) 
冬天旳寂寞 2024-12-19 19:05:06

我可以直接从列表框中获取项目计数吗?

这将给出许多被选择的项目

listbox.get_focus_widgets()

这将给出所选的索引和文本

listbox.focus_position
listbox.focus.base_widget.text

can I get the item count directly from the ListBox?

This will give many items being selected

listbox.get_focus_widgets()

This will give the selected index and text

listbox.focus_position
listbox.focus.base_widget.text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文