[Urwid] Fixed Size LineBox

Ian Ward ian at excess.org
Mon Jul 12 12:24:35 EDT 2010


Hello Muhammad,

See my reply below

Muhammad Ammar wrote on 2010-07-12 09:52:
> #!/usr/bin/env python
> 
> import urwid
> 
> #create palette
> palette = [('header', 'black', 'light gray'),
>            ('bg', 'black', 'light gray')]
> 
> 
> #create header(title message)
> txt_title = urwid.Text("\n\nSimple Database\n\n",
> 'center')               #is there any other way to place empty lines
> below and above the text instead of this

Yes, you could do something like

    txt_title = urwid.Pile([urwid.Divider(top=1),
        urwid.Text("Simple Database"),
        urwid.Divider(top=1)])

a divider is a blank line (by default) that you can add extra lines
above (top=1) or below.

> head = urwid.AttrMap(txt_title, 'header')
> 
> #create body
> txt_open = urwid.Text("Add Record ", 'center')
> txt_save = urwid.Text("Search Record", 'center')
> txt_exit = urwid.Text("Exit", 'center')
> 
> 
> menuList = urwid.SimpleListWalker([
>                                   txt_open
>                                   urwid.Divider(" "),
>                                   txt_save,
>                                   urwid.Divider(" "),
>                                   txt_exit
>                                 ])
> 
> body = urwid.LineBox(urwid.ListBox(menuList))

if you "know" the width you want the box to appear you could use a
Padding widget to center it with that size:

    body = urwid.Padding(body, 'center', 20)

Similarly you can use a Filler widget to set a fixed height for the
body:

    body = urwid.Filler(body, 'top', 7)

> 
> #create frame
> top = urwid.Frame(body, head)
> top = urwid.AttrMap(top, 'bg')
> 
> def exit_on_cr(input):
>     if input == 'enter':
>         raise urwid.ExitMainLoop()
>    
> loop = urwid.MainLoop(top, palette, unhandled_input=exit_on_cr)
> loop.run()
> 
> 
> Can anyone help me to draw the linebox as in figure-2?
> 
> second, i want to add multiple flow widgets in the footer area of Frame,
> IS THIS POSSIBLE? if yes/no, can any one kindly guide me to right direction?

A Pile like in the example I gave above would work.  See also:
http://excess.org/urwid/wiki/StandardWidgets

Ian



More information about the Urwid mailing list