简体   繁体   中英

Python Urwid Keystrokes

Good evening everybody,

I have an application built using Python Urwid library. It has several fields and hence takes quite some time to go to the bottom of the application using "Page Down" or "Down" keys. I'm just wondering if there is any keystrokes operations which directly take the cursor to the bottom. Something similar to this:

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
}

Thanks in advance.

There doesn't seem to be any equivalent mapping for that for the default widgets, if only because each app would probably have a different concept of what "the bottom" is.

What exactly do you mean by "the bottom"? Is there a widget that is always there, for which you want to give focus?

The container widgets have a writable focus_position attribute that you can use to change focus, here is an example:

#!/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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM