문제

나는 이것에 대해 검색했지만 내가 찾고있는 것은 아무것도 없습니다.

http://www.mail-archive.com/pygtk@daa.com.au/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

나는 그것이 내가 색상을 할당하는 방식이라고 생각했다. 스 니펫에서 볼 수 있듯이 그래픽 컨텍스트 자체 Colormap을 사용했습니다. 나는 다른 colormaps를 시도했는데 이것은 최신입니다. 나는 할당되지 않은 색상을 시도했습니다. White_GC 및 Black_GC 그래픽 컨텍스트에 주목하십시오. 사용하면 흰색 배경에 검은 색을 그리울 수 있습니다. 그렇지 않으면 (생성 된 컨텍스트와 함께) 모든 것이 검은 색, fg 및 bg입니다. White의 전경 색상을 변경하면 항상 검은 색으로 나옵니다.

출력은 다음과 같습니다. 색상이 크게 변하지 않는다는 것을 알 수 있습니다. 나는 그것이 바뀌지 않았거나 적어도 시각적으로 문제가되지 않을 것이라고 말하고 싶습니다.

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
도움이 되었습니까?

해결책

나는 약간의 문제가 있었다 그림을 그릴 수 있습니다 그리고 GC 과거에. 이 답변 솔루션으로가는 길을 시작했습니다. 다음은 사용자 정의 색상 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()

도움이되기를 바랍니다.

다른 팁

문제는 NewColor() 함수는 할당되지 않은 색상을 반환합니다 c. colormap.alloc_color() 반환 a gtk.gdk.Color 할당 된 색상입니다. 마지막 줄을 고치기 위해 NewColor() 해야한다:

return colormap.alloc_color(c)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top