问题
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