简体   繁体   English

urwid即时更改调色板颜色

[英]urwid change palette colors on the fly

In urwid how can I change colors of a palette on the fly? 在urwid中,如何动态更改调色板的颜色? For example, let's say I wanted change when I press the 'C' button: 例如,假设我想按“ 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()

You can use register_palette_entry . 您可以使用register_palette_entry It's a method found in Screen , which is available as a public member of the MainLoop . 这是Screen的一种方法,可以作为MainLoop的公共成员使用。

After calling this method with parameters of choice, make sure to redraw the screen - for example, with screen.clear() . 在使用选择的参数调用此方法之后,请确保重绘屏幕-例如,使用screen.clear()

Below is a working example - hit c to flip between light red and light gray background. 下面是一个工作示例-按下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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM