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

Kent Johnson kent37 at tds.net
Thu Sep 18 00:09:13 CEST 2008


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()


More information about the Tutor mailing list