首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Urwid击键

Python Urwid击键
EN

Stack Overflow用户
提问于 2018-03-26 23:42:22
回答 1查看 434关注 0票数 0

大家晚上好,

我有一个使用Python Urwid库构建的应用程序。它有几个字段,因此使用"Page Down“或"Down”键转到应用程序的底部需要相当长的时间。我只是想知道是否有直接将光标移到底部的按键操作。类似于下面的内容:

代码语言:javascript
复制
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
}

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-03-27 01:50:03

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

你说的“底部”到底是什么意思?有没有一个小部件总是在那里,你想让它成为焦点?

container widgets具有一个可写focus_position属性,您可以使用该属性来更改焦点,下面是一个示例:

代码语言:javascript
复制
#!/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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49495437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档