【问题标题】:Urwid ListBox: How to get fluid focus movement?Urwid ListBox:如何获得流畅的焦点移动?
【发布时间】:2023-03-18 08:26:01
【问题描述】:

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

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 并聚焦的行为。实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: python user-interface console curses urwid


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      class SelectableText(urwid.Text):
          ...
          def selectable(self):
              return True
      ...
      
      items = map(lambda x: SelectableText(`x`), range(500))
      

      【讨论】:

      • 这允许它是可选择的,但我的 impl 仍然告诉我它需要一个按键,但没有定义按键。我定义了这一点,但是即使使用 SimpleFocusListWalker 也无法为 ListBox 制作 Text
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      相关资源
      最近更新 更多