Urwid ListBox:如何获得流畅的焦点移动?

发布于 2024-12-15 01:02:48 字数 670 浏览 2 评论 0原文

我有以下代码片段,显示数字列表,并突出显示当前焦点项目:

import urwid

palette = [('header', 'white', 'black'),
    ('reveal focus', 'black', 'dark cyan', 'standout'),]

items = map(lambda x: urwid.Text(`x`), range(500))
items = map(lambda x: urwid.AttrMap(x, None, 'reveal focus'), items)

walker = urwid.SimpleListWalker(items)
listbox = urwid.ListBox(walker)

loop = urwid.MainLoop(listbox, palette)
loop.run()

当我启动程序时,终端看起来像:

0   <-- highlighted
1
2
3
...

如果我按 down 按钮,则视图将更改为:

1
2
3
4   <-- highlighted
...

我想要一种在屏幕向下滚动之前突出显示并聚焦 0-3 的行为。实现这一目标的最佳方法是什么?

I have the following code snippet that shows a list of numbers, and highlights the currently in-focus item:

import urwid

palette = [('header', 'white', 'black'),
    ('reveal focus', 'black', 'dark cyan', 'standout'),]

items = map(lambda x: urwid.Text(`x`), range(500))
items = map(lambda x: urwid.AttrMap(x, None, 'reveal focus'), items)

walker = urwid.SimpleListWalker(items)
listbox = urwid.ListBox(walker)

loop = urwid.MainLoop(listbox, palette)
loop.run()

When I start the program the terminal looks like:

0   <-- highlighted
1
2
3
...

If I press the down button then the view changes to:

1
2
3
4   <-- highlighted
...

I would like a behavior in which 0-3 are highlighted and in-focus before the screen scrolls downward. What is the best way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半夏半凉 2024-12-22 01:02:48

接受的解决方案在第一次按键时失败:

AttributeError: 'SelectableText' object has no attribute 'keypress'

注入我自己的实现,该实现不执行任何操作会使错误消失,但它也完全破坏了所有按键绑定。

我发现了这个,它似乎工作得很好:

class SelectableText(urwid.Edit):
    def valid_char(self, ch):
        return False

The accepted solution fails on first key press:

AttributeError: 'SelectableText' object has no attribute 'keypress'

Injecting my own implementation that does nothing makes the error go away, but it also completely borks all keybindings.

I found this and it seems to work just fine:

class SelectableText(urwid.Edit):
    def valid_char(self, ch):
        return False
π浅易 2024-12-22 01:02:48

列表框中的项目必须是可选择的。这是一种方法:

class SelectableText(urwid.Text):
    ...
    def selectable(self):
        return True
...

items = map(lambda x: SelectableText(`x`), range(500))

The items in the list box need to be selectable. Here is one approach:

class SelectableText(urwid.Text):
    ...
    def selectable(self):
        return True
...

items = map(lambda x: SelectableText(`x`), range(500))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文