[Tutor] clearing lines for a 'front end' to a tool

James jtp at nc.rr.com
Thu Sep 18 00:20:44 CEST 2008


Kent,

Thanks for the response. As a personal opinion, do you find it more
appropriate to use something like curses (since it's already built
into the standard Python library), or to use something "third party"
like urwid?

This program will be distributed to lots of individuals that will
likely not have urwid installed right off the bat.

I find the Python curses documentation to be...lacking at best. ;)
Thus the only benefit I see to curses over urwid is that it is already
"built-in" to Python.

-j

On Wed, Sep 17, 2008 at 6:09 PM, Kent Johnson <kent37 at tds.net> wrote:
> On Wed, Sep 17, 2008 at 5:02 PM, James <jtp at nc.rr.com> wrote:
>> Kent / Alan,
>>
>> Thanks for the responses. I'm not completely certain that urwid is
>> appropriate for the program I'm writing, as it seems to be more of a
>> framework for developing a text GUI application. (similar to curses?)
>
> It takes less code to do what you want in urwid. You don't have to
> create menus. Here is an example that is I think close to what you
> want. It displays a header line, a scrolling log and a fixed status
> line. I have never used urwid before and I whipped this up in about
> 1/2 hour so don't take it as gospel. It seems well-behaved, even
> handles resizing the terminal window.
>
> Kent
>
> # Use urwid to display a scrolling log and a fixed status line
>
> import time
> import urwid.curses_display
> import urwid
>
> class ScrollingStatus(object):
>    def __init__(self):
>        # This contains the log items
>        self.items = urwid.SimpleListWalker([])
>        self.listbox = urwid.ListBox(self.items)
>
>        # A header line so we know how to quit...
>        instruct = urwid.Text("Press Esc to exit.")
>        header = urwid.AttrWrap( instruct, 'header' )
>
>        # The status line
>        self.status_line = urwid.Text('Count = 0')
>
>        # Wrap it all up in a Frame
>        self.top = urwid.Frame(self.listbox, header,
> urwid.AttrWrap(self.status_line, 'status'))
>
>        # This is our 'status' data
>        self.count = 0
>
>    def main(self):
>        self.ui = urwid.curses_display.Screen()
>        self.ui.register_palette([
>            ('header', 'black', 'dark cyan', 'standout'),
>            ('status', 'black', 'light gray', 'standout'),
>            ])
>        self.ui.run_wrapper( self.run )
>
>    def run(self):
>        size = self.ui.get_cols_rows()
>
>        while True:
>            self.draw_screen( size )
>
>            keys = self.ui.get_input()
>            if "esc" in keys:
>                break
>
>            if "window resize" in keys:
>                size = self.ui.get_cols_rows()
>
>            # Add a log item
>            self.items.append(urwid.Text('The time is %s' % time.asctime()))
>            self.items.set_focus(self.items.get_focus()[1] + 1)
>
>            # Update the status line
>            self.count += 1
>            self.status_line.set_text('Count = %s' % self.count)
>
>            time.sleep(1)
>
>    def draw_screen(self, size):
>        canvas = self.top.render( size, focus=True )
>        self.ui.draw_screen( size, canvas )
>
> ScrollingStatus().main()
>



-- 
"Do not meddle in the affairs of wizards, for they are subtle and
quick to anger."
   -Tolkien


More information about the Tutor mailing list