make child widget get the input keypress with urwid on python

被刻印的时光 ゝ 提交于 2021-01-01 09:09:12

问题


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.

Like in this functional example:

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()

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.

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


回答1:


In NewFrame's keypress() method call master_pile.keypress(), as below:

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

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


来源:https://stackoverflow.com/questions/65018591/make-child-widget-get-the-input-keypress-with-urwid-on-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!