1

在urwid中,我如何动态更改调色板的颜色?例如,假设我想在按下“C”按钮时进行更改:

import urwid

def changeColor(key):
    if key in ('c', 'C'):
        c = "light gray"

c = 'black'

palette = [("text", "black", c)]

text = urwid.Text(("text", u'Hello humans'), align='center')
fill = urwid.Filler(text)
urwid.MainLoop(fill, palette, unhandled_input=changeColor).run()
4

1 回答 1

2

您可以使用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()
于 2016-02-23T10:16:28.007 回答