2016-03-06 54 views
0

我需要幫助理解着色如何工作的urwidPython的URWID調色板

color_palette = [ 
    ('banner', '', '', '', '#fff', 'g35'), 
    ('streak', 'white', '', '', 'g0', 'g35'), 
    ('inside', '', '', '', 'g0', 'g35'), 
    ('outside', '', '', '', 'g0', 'g35'), 
    ('bg', '', '', '', 'g35', '#fff'),] 

從文檔: http://urwid.org/manual/displayattributes.html#id6

顏色我想用:

- #195c60 
- #193638 
- #232323 

用法:http://urwid.org/tutorial/index.html#high-color-modes

import urwid 

def exit_on_q(key): 
    if key in ('q', 'Q'): 
     raise urwid.ExitMainLoop() 

palette = [ 
    ('banner', '', '', '', '#ffa', '#60d'), 
    ('streak', '', '', '', 'g50', '#60a'), 
    ('inside', '', '', '', 'g38', '#808'), 
    ('outside', '', '', '', 'g27', '#a06'), 
    ('bg', '', '', '', 'g7', '#d06'),] 

placeholder = urwid.SolidFill() 
loop = urwid.MainLoop(placeholder, palette, unhandled_input=exit_on_q) 
loop.screen.set_terminal_properties(colors=256) 
loop.widget = urwid.AttrMap(placeholder, 'bg') 
loop.widget.original_widget = urwid.Filler(urwid.Pile([])) 

div = urwid.Divider() 
outside = urwid.AttrMap(div, 'outside') 
inside = urwid.AttrMap(div, 'inside') 
txt = urwid.Text(('banner', u" Hello World "), align='center') 
streak = urwid.AttrMap(txt, 'streak') 
pile = loop.widget.base_widget # .base_widget skips the decorations 
for item in [outside, inside, streak, inside, outside]: 
    pile.contents.append((item, pile.options())) 

loop.run() 
+1

什麼是你的問題? – zondo

+0

@zondo我如何使用我想要的顏色? –

+1

@你有什麼問題?我們不能告訴你*爲什麼*如果我們不知道*你的代碼不起作用。 – zondo

回答

1

這聽起來像你想用你自定義顏色的urwid高顏色示例。

在本教程的例子,下面一行告訴終端使用8位終端的顏色模式(https://en.wikipedia.org/wiki/8-bit_color):

loop.screen.set_terminal_properties(colors=256) 

要使用自定義顏色,您需要將您的(大概RGB)轉換顏色到最接近的可能對應的8位終端顏色。請注意,您不一定能夠獲得完全匹配:有方式更多RGB十六進制顏色代碼比8位終端顏色代碼。

我用這個工具來做到找到你的顏色近似匹配:https://gist.github.com/MicahElliott/719710

$ colortrans 195c60 
RGB 195c60 -> xterm color approx 23 (005f5f) 
$ colortrans 193638 
RGB 193638 -> xterm color approx 23 (005f5f) 
$ colortrans 232323 
RGB 232323 -> xterm color approx 16 (000000) 

擁有現代化的urwid,那麼你可以做:

palette = [ 
    ('color1', '', '', '', 'h23', 'h23'), 
    ('color2', '', '', '', 'h16', 'h16'),]