Python Forum

Full Version: Refresh test in urwid.Text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying output some test to the screen, but I have to update the text when some external data changes.

So I made this test program, and even though it works, I think I am doing something very wrong, since it seems like I am creating loops within loops.

Can someone tell me what i am doing wrong?

#!/usr/bin/python
import urwid

def unhandled_input(key):
    if key == 'q':
        raise urwid.ExitMainLoop()

def refresh(_loop,_data):
	outputTxt       = ['COMP3      ', (red_bg, 'DOWN   '), "Bla\n"]
	outputTxt      += ['COMP4      ', (green_bg, 'UP     '), "Bla bla\n"]

	txt             = urwid.Text(outputTxt)
	fill            = urwid.Filler(txt, 'top')
	loop            = urwid.MainLoop(fill, unhandled_input=unhandled_input)
	loop.set_alarm_in(1,refresh)
	loop.run()

red_bg = urwid.AttrSpec('default', 'dark red')
green_bg = urwid.AttrSpec('default', 'dark green')

outputTxt       = ['COMP1      ', (red_bg, 'DOWN   '), "Bla\n"]
outputTxt      += ['COMP2      ', (green_bg, 'UP     '), "Bla bla\n"]

txt             = urwid.Text(outputTxt)
fill            = urwid.Filler(txt, 'top')
loop            = urwid.MainLoop(fill, unhandled_input=unhandled_input)
loop.set_alarm_in(1,refresh)
loop.run()
Found the solution, had to change the refresh method to this.

def refresh(_loop,_data):
    outputTxt       = ['COMP3      ', (red_bg, 'DOWN   '), "Bla\n"]
    outputTxt      += ['COMP4      ', (green_bg, 'UP     '), "Bla bla\n"]

    txt.set_text(outputTxt)
    loop.set_alarm_in(1,refresh)