How do I get urwid to render a Text widget nested inside a Filler and a WidgetWrap?

344 Views Asked by At

My goal is to wrap Urwid's text widget to create my own custom widget. To do this, I need to wrap the text widget in a Filler widget so I can render it the correct size.

With this example code, I'm expecting to see "test" on my screen, then the key that I press if they're one of "wasd". Instead, I see nothing. Logging is telling me that I'm hitting my code in keypress, and previous debugging showed that I was hitting the render() function of the text widget and returning the correct value.

import logging
import urwid

logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logger = logging.getLogger(__name__)


class TestWidget(urwid.WidgetWrap):
    def __init__(self):
        self.text_widget = urwid.Text("test")
        super().__init__(urwid.Filler(self.text_widget, valign='top', height='flow'))
        self._selectable = True

    def selectable(self):
        return self._selectable

    def keypress(self, size, key):
        if key in ('w', 's', 'a', 'd'):
            logger.info(key)
            self.text_widget.set_text(key)
        else:
            return key

def show_or_exit(key):
    if key in ('q', 'Q'):
        raise urwid.ExitMainLoop()

if __name__ == '__main__':
    logger.info("Starting")
    wid = TestWidget()
    loop = urwid.MainLoop(wid, unhandled_input=show_or_exit, handle_mouse=True)
    loop.run()
1

There are 1 best solutions below

0
raek On

Hm. I don't see anything wrong with the code and it worked for me when I ran it in Python 3.5.2 and Urwid 2.1.2 and Urwid 1.3.1 (I tried both). I saw a single line displaying "test" which swiched to display "w" etc after keypresses.

(I had to remove the encoding keyword argument to basicConfig, but that doesn't seem relevant to Urwid).

What versions of Python and Urwid did you use?