3

这很奇怪,但是当我运行程序时,它只更新了 20 次屏幕中的 1 次......而且如果我按任意键 - 它也不会更新。如果像python code.py 2>log. 而且mylog.log我用于内部日志记录 - 它说refresh()工作正确......

import urwid
from random import randint
import thread,logging
from time import sleep
logging.basicConfig(format = u'%(levelname)-8s [%(asctime)s] %(message)s', level = logging.CRITICAL, filename = u'mylog.log')

class ArRow (urwid.WidgetWrap):
    def __init__ (self):
        self.build_row()
        w = urwid.Columns(self.item)
        self.__super.__init__(w)
    def build_row(self):
        w=5
        self.item = [
            ('fixed',w,urwid.AttrWrap(urwid.Text(str(randint(1,100))), 'body', 'focus')),
            ('fixed',w,urwid.AttrWrap(urwid.Text(str(randint(1,100))), 'body', 'focus')),
            ('fixed',w,urwid.AttrWrap(urwid.Text(str(randint(1,100))), 'body', 'focus')),
        ]
    def refresh(self):
        self.build_row()
        w = urwid.Columns(self.item)
        self.__super.__init__(w)
        logging.critical(str(self.item))
    def selectable (self):
        return True
    def keypress(self, size, key):
        return key

palette = [
    ('body','dark cyan', '', 'standout','#0f6',''),
    ('focus','dark red', '', 'standout','light red',''),
    ]

def keystroke (input):
    if input in ('q', 'Q'):
        raise urwid.ExitMainLoop()

items=[ArRow()]#,ArRow(),ArRow(),ArRow(),ArRow()]
listbox = urwid.ListBox(urwid.SimpleListWalker(items))
view = urwid.Frame(urwid.AttrWrap(listbox, 'body'))
loop = urwid.MainLoop(view, palette, unhandled_input=keystroke)
loop.screen.set_terminal_properties(colors=256)

def live_report(*a):
    while True:
        items[0].refresh()
        loop.draw_screen()
        sleep(2)

thread.start_new(live_report,(0,))
loop.run()
4

2 回答 2

1

解决方案:

class ArRow (urwid.WidgetWrap):
    def __init__ (self):
        self.build_row()
        self._w = AColumns(self.item)
        self.__super.__init__(self._w)
    def build_row(self):
        w=5
        self.item = [
            ('fixed',w,urwid.AttrWrap(urwid.Text(str(randint(1,100))), 'body', 'focus')),
            ('fixed',w,urwid.AttrWrap(urwid.Text(str(randint(1,100))), 'body', 'focus')),
            ('fixed',w,urwid.AttrWrap(urwid.Text(str(randint(1,100))), 'body', 'focus')),
        ]
    def refresh(self):
        self.build_row()
        self._w = AColumns(self.item)
        #self.__super.__init__(w)
        logging.critical(str(self.item))
......

def refresh(_loop,_data):
    for item in items:
        item.refresh()
    #loop.draw_screen()
    _loop.set_alarm_in(2,refresh)

loop.set_alarm_in(2,refresh)
loop.run()
于 2012-08-20T21:51:20.093 回答
1

你为什么不只是

import threading


def refreshScreen(mainloop):
    while 1:
        logging.debug("looping")
        mainloop.draw_screen()
        time.sleep(1)


mainloop = urwid.MainLoop(top, palette)
refresh = Thread(target=refreshScreen, args=(mainloop,))
refresh.start()
mainloop.run()
于 2014-05-14T23:34:46.127 回答