0

晚上好大家,

我有一个使用 Python Urwid 库构建的应用程序。它有几个字段,因此使用“Page Down”或“Down”键需要相当长的时间才能进入应用程序的底部。我只是想知道是否有任何按键操作直接将光标带到底部。与此类似的东西:

class SimulationView(urwid.WidgetWrap):       (line 6)
{ 
def get_main_frame(self):                     (line 127)
buttons_box = urwid.ListBox(buttons_walker)   (line 148)
errors_box = urwid.ListBox(self.errors_content) (line 155)
sim_listbox = urwid.ListBox(self.sim_list_content)(line 158)

body = urwid.Pile([(6, buttons_box),('weight', 4, sim_listbox),
                       ('weight', 1, errors_box)])
frame = urwid.Frame(body, header=header)
     return frame


def keypress(self, size, key): 

   If key is "a": 
      # command to take the cursor to the bottom of the application
}

提前致谢。

4

1 回答 1

1

对于默认小部件,似乎没有任何等效的映射,只是因为每个应用程序可能对“底部”是什么有不同的概念。

“底部”到底是什么意思?是否有一个始终存在的小部件,您想要关注它?

容器小部件有一个可写focus_position属性,您可以使用它来更改焦点,这是一个示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function, absolute_import, division
import urwid


def global_input(key):
    if key in ('q', 'Q', 'esc'):
        raise urwid.ExitMainLoop()
    elif key == 'page down':
        # "bottom" is last button, which is before footer
        pile.focus_position = len(pile.contents) - 2
    elif key == 'page up':
        # "top" is the first button, which is after footer
        pile.focus_position = 1
    elif key in ('1', '2', '3', '4'):
        pile.focus_position = int(key)


if __name__ == '__main__':
    footer = urwid.Text('Footer')
    pile = urwid.Pile([
        urwid.Padding(urwid.Text('Header'), 'center', width=('relative', 6)),
        urwid.Button('Button 1'),
        urwid.Button('Button 2'),
        urwid.Button('Button 3'),
        urwid.Button('Button 4'),
        urwid.Padding(footer, 'center', width=('relative', 20)),
    ])
    widget = urwid.Filler(pile, 'top')
    loop = urwid.MainLoop(widget, unhandled_input=global_input)
    loop.run()
于 2018-03-26T17:50:02.873 回答