pythonpython-3.xtuiurwid

Why does this simple "hello world" Urwid code fail?


I have a super simple program that uses Urwid for text-based UIs:

import urwid


title = urwid.Text('Test')

pile = urwid.Pile([
    title,
])

body = urwid.Frame(pile)

urwid.MainLoop(body).run()

However when I run it, I get an error like so:

/home/user/anaconda3/envs/torch-env/bin/python /home/user/Work/market-maker/test.py
Traceback (most recent call last):
  File "/home/user/Work/market-maker/test.py", line 97, in <module>
    urwid.MainLoop(body).run()
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/main_loop.py", line 287, in run
    self._run()
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/main_loop.py", line 385, in _run
    self.event_loop.run()
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/main_loop.py", line 790, in run
    self._loop()
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/main_loop.py", line 818, in _loop
    self._entering_idle()
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/main_loop.py", line 779, in _entering_idle
    callback()
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/main_loop.py", line 574, in entering_idle
    self.draw_screen()
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/main_loop.py", line 588, in draw_screen
    canvas = self._topmost_widget.render(self.screen_size, focus=True)
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/widget.py", line 145, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/container.py", line 1089, in render
    body = self.body.render((maxcol, maxrow-ftrim-htrim),
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/widget.py", line 145, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/container.py", line 1565, in render
    canv = w.render((maxcol, rows), focus=focus and item_focus)
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/widget.py", line 145, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/user/anaconda3/envs/torch-env/lib/python3.8/site-packages/urwid/widget.py", line 1001, in render
    (maxcol,) = size
ValueError: too many values to unpack (expected 1)

Process finished with exit code 1

Why is that? What am I missing?


Solution

  • I managed to get it working by putting it inside a Filler object

    import urwid
    title = urwid.Text('Test')
    pile = urwid.Pile([
        title
    ])
    body = urwid.Filler(pile)
    loop = urwid.MainLoop(body)
    loop.run()