[Urwid] question : how to display python list using listbox

Alain Leufroy alain.leufroy at logilab.fr
Fri Sep 9 04:39:24 EDT 2011


Hi,

> I have two list ;
> mydataID = ['D01','D02','D04','D5']
> mydataTXT = [
>     'data one',
>     'data two',
>     'data three',
>     'data four',
>     'data five',
>     ]
>
> My question is , how to :
> 1. Devide the full screen size into two part (top and bottom)
> 2. the bottom part will display my lists as --> mydataID+' '+mydataTXT
>     i.e : 'D01 data one'
>     each UP/DOWN button stroke will move the higlight to another
> listbox row
> 3. the top part will have two line :
>     a. ID --> will display value of mydataID of currently highlighted row
>     b. TXT --> will display value of mydataTXT of currently highlighted row
>     i.e : if the highlit is at third row of listbox, the top part will
> display :
>         ID     = D01
>         TXT    = data three


You can try this - I'm sorry for colors :)

from urwid import *

class SelectableText(Text):
     """Just a selectable Text widget"""
     _selectable = True
     keypress = lambda self, size, key: key

class BottomWalker(SimpleListWalker):
     """
     * manage the specific data
     * automaticly create a selectable Text for each data set
     * emit 'focus changed' signal when focus is changed
     """
     signals = ['focus changed']
     def __init__(self, ids, txts):
         super(BottomWalker, self).__init__(zip(ids, txts))
     getvalues = SimpleListWalker.__getitem__
     def __getitem__(self, focus):
         return AttrWrap(SelectableText('%s %s' % self.getvalues(focus)), None, 'focus')
     def set_focus(self, focus):
         emit_signal(self, 'focus changed', self.getvalues(focus))
         return super(BottomWalker, self).set_focus(focus)

class Main(Pile):
     """
     * display the data at bottom using a list box
     * display a formated data set at top
     * automaticly update the top text when focus change at bottom (using signal)
     """
     def __init__(self, ids, txts):
         self.top = Filler(Text(''))
         self.bottom_walker = BottomWalker(ids, txts)
         self.bottom = ListBox(self.bottom_walker)

         contents = [('weight', 0.5, self.top),
                     ('weight', 0.5, AttrWrap(self.bottom, 'bottom'))]

         super(Main, self).__init__(contents)

         connect_signal(self.bottom_walker, 'focus changed', self.update_top)
         self.bottom_walker.set_focus(0) # force first signal

     def update_top(self, data):
         self.top.body.set_text('ID  = %s\nTXT = %s' % data)

def main():
     from urwid.raw_display import Screen
     PALETTE = [('bottom', 'dark red', 'light cyan'),
                 ('focus', 'light cyan', 'dark red')]
     mydataID = ['D01','D02','D04','D5']
     mydataTXT = [
         'data one',
         'data two',
         'data three',
         'data four',
         'data five',
     ]
     MainLoop(Main(mydataID, mydataTXT), PALETTE, Screen()).run()

if __name__ == '__main__':
     main()


-- 
Alain Leufroy
LOGILAB, Paris (France)
http://www.logilab.fr
Informatique scientifique & gestion de connaissances



More information about the Urwid mailing list