您可以使用register_palette_entry
. 它是 中的一种方法Screen
,可作为 的公共成员使用MainLoop
。
使用选择的参数调用此方法后,请确保重绘屏幕 - 例如,使用screen.clear()
.
下面是一个工作示例 - 点击c
以在浅红色和浅灰色背景之间切换。
import urwid
class Main(object):
def __init__(self):
self.flip = False
palette = [('text', 'black', 'light red')]
text = urwid.Text(('text', u'Hello humans'), align='center')
self.fill = urwid.Filler(text)
self.loop = urwid.MainLoop(self.fill, palette, unhandled_input=self.key_press)
self.loop.run()
def key_press(self, key):
if key in ('c', 'C'):
self.flip = not self.flip
self.loop.screen.register_palette_entry('text', 'black', ['light red', 'light gray'][self.flip])
self.loop.screen.clear()
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
Main()