使子小部件在python上使用urwid获取输入按键 [英] make child widget get the input keypress with urwid on python

查看:87
本文介绍了使子小部件在python上使用urwid获取输入按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在另一个窗口小部件中创建一个窗口小部件,例如,父亲urwid.Frame可能将bodyurwid.Pile作为孩子. 在这种情况下,当孩子不得不处理某些特定的其他键时,父亲应该处理一些输入键.

I can make a widget inside another widget, as example an urwid.Frame father could have as body an urwid.Pile widget as child. In this situation, the father should process some input keys when the child have to treat some specific others keys.

类似于此功能示例:

import urwid


class NewFrame(urwid.Frame):
    def __init__(self, givenBody):
        super().__init__(urwid.Filler(givenBody, "top"))

    def keypress(self, size, key):
        if key  in ('f'):
            print("We are in NewFrame object")

        return super(NewFrame, self).keypress(size, key)


class NewPile(urwid.Pile):
    def __init__(self, givenList):
        super().__init__(givenList)

    def keypress(self, size, key):
        if key  in ('p'):
            print("We are in NewPile object")

        return super(NewPile, self).keypress(size, key)



master_pile = NewPile([
    urwid.Text("foo"),
    urwid.Divider(u'─'),
])


frame = NewFrame(master_pile)

loop = urwid.MainLoop(frame)
loop.run()

当我按下 f 时,我可以看到Text小部件我们在NewFrame中".但是,当我按 p 时,不会出现NewPile文字,并且什么也没有发生.

When I press f I could see the Text widget "We are in NewFrame". But when I press p, the NewPile text doesn’t appear and nothing happens.

那么,如何使子小部件获得输入键,尤其是当它们与父级的.keypress()方法不匹配时?

So, how could I make the child widget get input keys, especially when they are not matched by the .keypress() method of the parent?

推荐答案

从我从文档中可以了解到的内容( http://urwid.org/manual/widgets.html#pile-widgets )桩"小部件仅在包含另一个可选小部件时才可选(在您的示例中没有)

From what I can read from the documentation (http://urwid.org/manual/widgets.html#pile-widgets) a Pile widget is only selectable when it contains another selectable widget (which it doesn't in your example).

通过覆盖selectable()使其始终处于可选状态,您应该能够在子类中覆盖此行为.

You should be able to override this behavior in your subclass by overriding selectable() to make it always selectable.

我认为当窗口小部件不可选择时,Urwid将永远不会调用keypress,因为只有当窗口小部件不可选择时,窗口小部件才需要实现keypress(

I think Urwid will never call keypress when a widget is not selectable, because widgets are only required to implement keypress when thet are selectable (http://urwid.org/reference/widget.html#urwid.Widget.selectable).

这篇关于使子小部件在python上使用urwid获取输入按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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