很奇怪,但是当我运行程序时它只更新屏幕中的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()
答案 0 :(得分: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()
答案 1 :(得分: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()