“焦点"是指列表中的窗口小部件,但将光标保持在另一个窗口中 [英] "focus" widget in list but keep cursor in another

查看:61
本文介绍了“焦点"是指列表中的窗口小部件,但将光标保持在另一个窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 urwid ,我试图分离 Pile 小部件的突出显示/行走和光标功能.如何使用上/下更改突出显示的窗口小部件,同时将光标保持在其他窗口小部件中?

using urwid, I'm trying to separate the highlight/walk and cursor functionality of a Pile widget. How can I use up/down to change which widget is highlighted, while keeping the cursor in a different widget?

推荐答案

默认的 focus 行为将光标与属性(突出显示)行为耦合在一起.下面的示例显示了一种将它们解耦的方法,其中 SelectableIcons 列表保留突出显示功能,而将光标移到单独的 Edit 小部件上.它是通过以下方式完成的:

The default focus behavior couples the cursor with attribute (highlighting) behavior. The example below shows one way to decouple these, where a list of SelectableIcons retains the highlight feature, while the cursor is moved to a separate Edit widget. It does this via:

  • 覆盖 keypress 方法以更新光标不在的焦点
  • AttrMap 中包装每个 SelectableIcon ,它们根据其 Pile's focus_position 更改其属性代码>
  • 更改SelectableIcon属性后,通过 focus_part ='body'
  • 将焦点(光标)设置回 Edit 小部件.调用
  • self._w = ... 来更新屏幕上的所有小部件
  • overriding the keypress method to update the focus where the cursor is not
  • wrapping each SelectableIcon in AttrMap that change their attribute based on their Pile's focus_position
  • after changing the SelectableIcon attributes, the focus (cursor) is set back to the Edit widget via focus_part='body'
  • self._w = ... is called to update all widgets on screen

可能会有更简洁的方法,但这应该相当灵活.

There may be more concise ways of doing this, but this should be rather flexible.

import urwid

def main():
    my_widget = MyWidget()
    palette = [('unselected', 'default', 'default'),
               ('selected', 'standout', 'default', 'bold')]

    urwid.MainLoop(my_widget, palette=palette).run()


class MyWidget(urwid.WidgetWrap):

    def __init__(self):

        n = 10       
        labels = ['selection {}'.format(j) for j in range(n)]

        self.header = urwid.Pile([urwid.AttrMap(urwid.SelectableIcon(label), 'unselected', focus_map='selected') for label in labels])

        self.edit_widgets = [urwid.Edit('', label + ' edit_text') for label in labels]

        self.body = urwid.Filler(self.edit_widgets[0])

        super().__init__(urwid.Frame(header=self.header, body=self.body, focus_part='body'))

        self.update_focus(new_focus_position=0)

    def update_focus(self, new_focus_position=None):
        self.header.focus_item.set_attr_map({None: 'unselected'})

        try:
            self.header.focus_position = new_focus_position
            self.body = urwid.Filler(self.edit_widgets[new_focus_position])

        except IndexError:
            pass

        self.header.focus_item.set_attr_map({None: 'selected'})

        self._w = urwid.Frame(header=self.header, body=self.body, focus_part='body')

    def keypress(self, size, key):

        if key == 'up':
            self.update_focus(new_focus_position=self.header.focus_position - 1)

        if key == 'down':
            self.update_focus(new_focus_position=self.header.focus_position + 1)

        if key in {'Q', 'q'}:
            raise urwid.ExitMainLoop()

        super().keypress(size, key)

main()

这篇关于“焦点"是指列表中的窗口小部件,但将光标保持在另一个窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆