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 に答える