Pygtk 图形上下文与颜色分配

2 投票
2 回答
2008 浏览
提问于 2025-04-15 11:59

我搜索过这个问题,但没有找到我想要的答案。

http://www.mail-archive.com/pygtk@daa.com.au/msg10529.html -- 没有人回答他。这正是我遇到的情况。当我在图形上下文中设置前景色时,似乎并没有实际改变。

我看过教程和常见问题解答,但都没有提供太多信息。要么只是使用黑白的上下文,要么给你一些失效的链接。我开始觉得这可能是个bug。但我心里又觉得我可能漏掉了什么,而且我一直忽视了我其实有一个可用的替代方案。不过,这样会更好。随着我深入这个问题,我会越来越需要这些上下文和颜色。

这是我的代码片段。

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

我想也许是我分配颜色的方式有问题。正如你在代码片段中看到的,我使用了图形上下文自己的颜色映射。我尝试了不同的颜色映射,这是最新的一个。我甚至尝试了一个未分配的颜色。注意白色和黑色的图形上下文。当我使用这些时,我可以在白色背景上正常绘制黑色。否则(使用创建的上下文),一切都是黑色的,前景和背景都是黑色。当我改变白色的前景色时,它总是变成黑色。

这是输出结果。注意颜色几乎没有变化。我可以说它没有变化,或者至少变化得不够明显。

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 个回答

1

问题在于,NewColor() 这个函数返回了一个没有分配的颜色 c。而 colormap.alloc_color() 返回的是一个已经分配好的颜色 gtk.gdk.Color。要解决这个问题,NewColor() 函数的最后一行应该改成:

return colormap.alloc_color(c)
2

我之前在使用 DrawableGC 时遇到了一些麻烦。这个回答让我找到了问题的解决方向。下面是一个简单的例子,使用自定义颜色的 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()

希望这对你有帮助。

撰写回答