Ninius86
Ninius86

Reputation: 295

How to update displayed text in urwid?

Got confused with urwid. By adapting one of the urwid examples I get initial screen and would like to execute run_stuff() function when F5 key is pressed and also display message indicating that run_stuff() is running and eventually finished (ideally with progress bar, but just displaying message will do).

My code so far is below, something tells me running second main loop is not a good idea, but could not figure out other way to update screen with new text.

import urwid
import urwid.raw_display
import urwid.web_display
import os, time

def run_stuff():
    counter = 0
    for x in range(10):
        time.sleep(1)
        counter += 1
        # update progress bar: processing $counter out of 10

def main():   
    text_header = (u"Some header here!  "
        u"UP / DOWN / PAGE UP / PAGE DOWN scroll. F5 Run F8 exits.")

    initial_text = [u"Here is ",('important', u"Initial Text."), ]
    secondary_text = [u"Please wait while stuff is running" ]
    options_list = [u"option1", u"option2", u"option3" ]

    radio_button_group = []

    blank = urwid.Divider()
    listbox_content = [
        blank,
        urwid.Padding(urwid.Text(initial_text), left=2, right=2, min_width=20),
        blank,

        urwid.Padding(urwid.GridFlow(
            [urwid.AttrWrap(urwid.RadioButton(radio_button_group,
                txt), 'buttn','buttnf')
                for txt in options_list],
            28, 7, 1, 'left') ,
            left=3, right=3, min_width=20),
        blank,
        blank,
    ]

    header = urwid.AttrWrap(urwid.Text(text_header), 'header')
    listbox = urwid.ListBox(urwid.SimpleListWalker(listbox_content))
    frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'), header=header)

    palette = [
        ('body','black','light gray', 'standout'),
        ('reverse','light gray','black'),
        ('header','white','dark red', 'bold'),
        ('important','dark blue','light gray',('standout','underline')),
        ('editfc','white', 'dark blue', 'bold'),
        ('editbx','light gray', 'dark blue'),
        ('editcp','black','light gray', 'standout'),
        ('bright','dark gray','light gray', ('bold','standout')),
        ('buttn','black','dark cyan'),
        ('buttnf','white','dark blue','bold'),
        ]


    # use appropriate Screen class
    if urwid.web_display.is_web_request():
        screen = urwid.web_display.Screen()
    else:
        screen = urwid.raw_display.Screen()

    def unhandled(key):
        if key == 'f8':
            raise urwid.ExitMainLoop()
        elif key == 'f5':

            listbox_content = [
                blank,
                urwid.Padding(urwid.Text(secondary_text), left=2, right=2, min_width=20),
                blank,
                blank
                ]

            header = urwid.AttrWrap(urwid.Text(text_header), 'header')
            listbox = urwid.ListBox(urwid.SimpleListWalker(listbox_content))
            frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'), header=header)
            urwid.MainLoop(frame, palette, screen, unhandled_input=unhandled).run()

            run_stuff()

    urwid.MainLoop(frame, palette, screen, unhandled_input=unhandled).run()

def setup():
    urwid.web_display.set_preferences("Urwid Tour")
    # try to handle short web requests quickly
    if urwid.web_display.handle_short_request():
        return

    main()

if '__main__'==__name__ or urwid.web_display.is_web_request():
    setup()

Upvotes: 4

Views: 1842

Answers (1)

Joseph Sheedy
Joseph Sheedy

Reputation: 6726

One solution would be to define secondary_text as a urwid.Text object in the global scope, then call its set_text method from within run_stuff().

Upvotes: 4

Related Questions