使用できます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()