Urwid for Python, a ncurses library
This, is the first part of a six posts series on Urwid.
- Urwid for Python, a ncurses library
- Selectable List with Urwid for Python
- Signals with Urwid for Python
- Footer Edit with Urwid, IRC-Client-like input
- Several colored string with Urwid
- HTML output screenshot with Urwid ncurses library
I really like ncurses programs, it’s certainly what I use the most for an every day usage. The curses module from Python provides some elements to build an application, it’s what I used for my last program Tyrs. Unfortunately, the module doesn’t provide any widgets or advances elements, usually instead of reinvents the wheel, we use a library. As far as I know, Python have only two libraries for ncurses.
- PyCDK is some alpha stage library, who doesn’t seem to be maintain anymore. It’s originally a binding of the CDK library for C. I tried it, but I didn’t manage to get it work, maybe I was unlucky. Anyway, I would not advise this library.
- Urwid is a serious library, who’s come with lots of features and widgets, and will be the subject of this post.
Unfortunately, their isn’t any others libraries that those two.
It’s always interesting when starting consider a library to know if their are others programs using it too, a list is provide by the website, with some interesting programs, such as wicd-curses, hachoir and the Debian “reportbug” tool.
The most interesting pieces of example to get start with come from this page, a set of small programs with screenshot.
Urwid come with lots of features and widget, allow you to display a nice layout easily, editing text, 256 colors, utf-8, check box, advance list management, mouse event and much more. The documentation and tutorial is well documented.
Urwid is maybe not so easy to get start with if you’re not used to play with library, and maybe it’s why it isn’t more popular but have lots of potential. Urwid has became mature, and after seven years of development as reach the 1.0.0 release few months ago.
Here is a traditional Hello World, with a quit keystroke handling.
import urwid
txt = urwid.Text("Hello World")
fill = urwid.Filler(txt, 'top')
def exit(input):
if input in ('q', 'Q'):
raise urwid.ExitMainLoop()
loop = urwid.MainLoop(fill, unhandled_input=exit)
loop.run()
I will soon write some more posts about Urwid, with codes and more technical. This is only a short introduction of Urwid. I hope this post will give you enough reason to give Urwid a try.
About