Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygtk图形上下文和分配颜色_Python_Pygtk_Drawing2d - Fatal编程技术网

Python Pygtk图形上下文和分配颜色

Python Pygtk图形上下文和分配颜色,python,pygtk,drawing2d,Python,Pygtk,Drawing2d,我已经搜索过了,但没有找到我想要的 没有人回答他。这正是我所经历的。当我在图形上下文中设置前景时,它实际上似乎没有改变 我已经阅读了教程和常见问题解答,但都没有说太多。它们要么只是使用黑白上下文,要么提供断开的链接。我在想可能是个虫子。但我内心的孩子说我只是错过了一些东西,我一直忽视我有一个工作选择的事实。不过这样会更好。我越深入,我就越需要这些背景和颜色 这是我的代码片段 def CreatePixmapFromLCDdata(lcdP, ch, widget): width = lc

我已经搜索过了,但没有找到我想要的

没有人回答他。这正是我所经历的。当我在图形上下文中设置前景时,它实际上似乎没有改变

我已经阅读了教程和常见问题解答,但都没有说太多。它们要么只是使用黑白上下文,要么提供断开的链接。我在想可能是个虫子。但我内心的孩子说我只是错过了一些东西,我一直忽视我有一个工作选择的事实。不过这样会更好。我越深入,我就越需要这些背景和颜色

这是我的代码片段

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
下面是我如何分配颜色的

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绘制一些正方形的快速示例:

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()
返回分配的颜色
gtk.gdk.color
。要解决问题,
NewColor()
中的最后一行应该是:

return colormap.alloc_color(c)

嗯,设置rgb和fg颜色很有效。我从未在任何文档中看到过这种方法。谢谢你的提示。现在可以了,感谢上帝。我也有同样的问题。为什么要设置不起作用的前景,而设置不起作用的未记录的rgb\fg\U颜色。。。唉。。
return colormap.alloc_color(c)