2009-06-02 2 views
2

나는 이것을 조사했지만, 내가 찾고있는 것은 아무것도 없다.Pygtk 그래픽 컨텍스트와 색상 할당

http://www.mail-archive.com/[email protected]/msg10529.html - 아무도 그에게 대답하지 않았습니다. 이것이 바로 내가 경험 한 것입니다. 그래픽 컨텍스트에서 전경을 설정할 때 실제로 변경되지 않는 것 같습니다.

나는이 튜토리얼과 FAQ를 끝까지 읽었지만 둘 다 잘 알려져 있지 않다. 그들은 흑백 컨텍스트를 사용하거나 깨진 링크를 제공합니다. 어쩌면 그것이 버그라고 생각하게됩니다. 그러나 내 안에있는 아이는 내가 뭔가를 놓친다 고 말하고 나는 실제로 대안을 가지고 있다는 사실을 무시하고있다. 그래도 더 좋을거야. 그리고 더 깊숙이 들어가면 더 많은 컨텍스트와 색상이 필요할 것입니다.

내 코드 단편은 다음과 같습니다.

def CreatePixmapFromLCDdata(lcdP, ch, widget): 
    width = lcdP.get_char_width() 
    height = lcdP.get_char_height() 

    # Create pixmap 
    pixmap = gtk.gdk.Pixmap(widget.window, width, height) 

    # Working graphics contexts, wrong color 
    black_gc = widget.get_style().black_gc 
    white_gc = widget.get_style().white_gc 

    char_gc = widget.window.new_gc() 
    colormap = char_gc.get_colormap() 

    bg_color = NewColor(text="#78a878", colormap=colormap) 

    print "Before", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue 
    char_gc.set_foreground(bg_color) 
    print "AFter", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue 

    fg_color = NewColor(text="#113311", colormap=colormap) 

    pixmap.draw_rectangle(char_gc, True, 0, 0, width, height) 
    char_gc.foreground = fg_color 
    for j in range(lcdP.dots['y']): 
     k = lcdP.pixels['y']*j 
     for i in range(lcdP.dots['x']): 
      if 1<<(lcdP.dots['x']-1-i) & ch[j] == 0: continue 

      m = i*lcdP.pixels['y'] 

      for jj in range(k, k+lcdP.pixels['y']-1): 
       for ii in range(m+1, m+lcdP.pixels['x']): 
        pixmap.draw_point(char_gc, ii, jj) 
    return pixmap 

아마도 내가 색상을 할당하는 방식이라고 생각했습니다. 스 니펫에서 보았 듯이 그래픽 컨텍스트의 고유 한 색상 맵을 사용했습니다. 나는 다른 컬러 맵을 시도했는데, 이것이 최신 버전이다. 나는 심지어 할당되지 않은 색을 시도했다. white_gc 및 black_gc 그래픽 컨텍스트를 확인하십시오. 나는 그것들을 사용할 때 나는 흰 바탕에 검은 색을 멋지게 그릴 수있다. 그렇지 않으면 (작성된 컨텍스트에서) 모든 것이 검은 색이며, fg 및 bg입니다. 흰색의 전경색을 변경하면 항상 검정색으로 바뀝니다.

다음은 출력입니다. 색이 많이 변하지 않는 것을 주목하십시오. 나는 그것이 변하지 않았다고 말했고, 적어도 시각적으로는 문제가 없다고 말할 수 있습니다.

Before 6 174 60340 
After 5 174 60340 

다음은 색상을 할당 한 방법입니다.

def NewColor(red=0, green=0, blue=0, text=None, colormap=None): 
    if text == None: 
     c = gtk.gdk.Color(red, green, blue) 
    else: 
     c = gtk.gdk.color_parse(text) 
    if colormap == None: 
     colormap = gtk.gdk.colormap_get_system() 
    colormap.alloc_color(c) 
    return c 

답변

2

나는 과거에 DrawableGC와 함께 몇 가지 문제를 했어. This answer 솔루션으로가는 길에 나를 시작했습니다. 여기에 몇 가지 사각형을 그릴 사용자 정의 색상 GC를 사용하는 간단한 예는 다음과 같습니다

import gtk 

square_sz = 20 
pixmap = None 
colour = "#FF0000" 
gc = None 

def configure_event(widget, event): 
    global pixmap 
    x, y, width, height = widget.get_allocation() 
    pixmap = gtk.gdk.Pixmap(widget.window, width, height) 
    white_gc = widget.get_style().white_gc 
    pixmap.draw_rectangle(white_gc, True, 0, 0, width, height) 
    return True 

def expose_event(widget, event): 
    global pixmap 
    if pixmap: 
     x , y, w, h = event.area 
     drawable_gc = widget.get_style().fg_gc[gtk.STATE_NORMAL] 
     widget.window.draw_drawable(drawable_gc, pixmap, x, y, x, y, w, h) 
    return False 

def button_press_event(widget, event): 
    global pixmap, square_sz, gc, colour 
    if event.button == 1 and pixmap: 
     x = int(event.x/square_sz) * square_sz 
     y = int(event.y/square_sz) * square_sz 
     if not gc: 
      gc = widget.window.new_gc() 
      gc.set_rgb_fg_color(gtk.gdk.color_parse(colour)) 
     pixmap.draw_rectangle(gc, True, x, y, square_sz, square_sz) 
     widget.queue_draw_area(x, y, square_sz, square_sz) 

    return True 

if __name__ == "__main__": 
    da = gtk.DrawingArea() 
    da.set_size_request(square_sz*20, square_sz*20) 

    da.connect("expose_event", expose_event) 
    da.connect("configure_event", configure_event) 
    da.connect("button_press_event", button_press_event) 

    da.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK) 

    w = gtk.Window() 
    w.add(da) 
    w.show_all() 
    w.connect("destroy", lambda w: gtk.main_quit()) 

    gtk.main() 

는 도움이되기를 바랍니다.

+0

음, set_rgb_fg_color 작동합니다. 나는 어떤 문서에서도 그 방법을 본 적이 없다. 팁 고마워. 이제 작동합니다. – Scott

+0

감사합니다. 나는 정확히 똑같은 문제를 겪고 있었다. 왜 set_rgb_fg_color가 작동하지 않는 set_foreground를 가지고 있겠습니까? 한숨을 쉬게합니다. – Claudiu

1

NewColor() 함수가 할당되지 않은 색 c을 반환하는 것이 문제입니다. colormap.alloc_color()은 할당 된 색상 인 gtk.gdk.Color을 반환합니다. 일을 해결하려면 NewColor()의 마지막 행은 다음과 같습니다

return colormap.alloc_color(c)