Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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中的Pixmap透明度_Python_Transparency_Pygtk - Fatal编程技术网

Python PyGTK中的Pixmap透明度

Python PyGTK中的Pixmap透明度,python,transparency,pygtk,Python,Transparency,Pygtk,如何创建一个像素值设置为透明的PyGTK像素贴图?我知道它与创建深度为1的pixmap并将其设置为遮罩有关,但我发现它要么什么都不做,要么在绘制时完全擦除我的pixmap。现在,我用它做了一个pixmap r = self.get_allocation() p1 = gtk.gdk.Pixmap(self.window,r.width,r.height) p1_c = p1.cairo_create() 然后用Cairo在上面画一条黑线。我希望能够做到的是让所有没有被线条覆盖的区域都透明(比如

如何创建一个像素值设置为透明的PyGTK像素贴图?我知道它与创建深度为1的pixmap并将其设置为遮罩有关,但我发现它要么什么都不做,要么在绘制时完全擦除我的pixmap。现在,我用它做了一个pixmap

r = self.get_allocation()
p1 = gtk.gdk.Pixmap(self.window,r.width,r.height)
p1_c = p1.cairo_create()
然后用Cairo在上面画一条黑线。我希望能够做到的是让所有没有被线条覆盖的区域都透明(比如说,使白色成为透明的颜色),这样当我用draw_drawable将其绘制到窗口时,它会让“下面”的所有东西都完好无损


关于这个问题的常见问题解答和邮件列表帖子是最没有帮助的,因为它们太过时了。这里一定有人知道

看起来您想使用的是Pixbuf而不是Pixmap。Pixbuf包含一个alpha设置,它可以提供透明性,而Pixmap则没有。

我认为你不能用
Pixmap
Pixbuf
做你想做的事情,但是这里有两种策略可以在现有
小部件上实现涂鸦。最明显的一个方法是捕捉绘制事件,直接绘制到
小部件的
可绘制的
,中间没有保留的图像:

from gtk import Window, Button, main
from math import pi
import cairo

w = Window()
b = Button("Draw on\ntop of me!")

def scribble_on(cr):
    cr.set_source_rgb(0, 0, 0)
    cr.rectangle(10, 10, 30, 30)
    cr.fill()
    cr.arc(50, 50, 10, 0, pi)
    cr.stroke()

def expose_handler(widget, event):
    cr = widget.window.cairo_create()
    cr.rectangle(event.area.x, event.area.y,
                 event.area.width, event.area.height)
    cr.clip()
    scribble_on(cr)
    return False

b.connect_after("expose_event", expose_handler)
w.add(b)
w.set_size_request(100, 100)
w.show_all()
main()
第二个选项是,如果您希望有一个中间ARGB图像,而不必在每次请求重画时更新该图像,则可以将该图像预渲染到
imagessurface
。下面是上面的
expose\u handler
的替代品,它只绘制一次图像:

import cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
scribble_on(cairo.Context(surface))

def expose_image_handler(widget, event):
    cr = widget.window.cairo_create()
    cr.rectangle(event.area.x, event.area.y,
                 event.area.width, event.area.height)
    cr.clip()
    cr.set_source_surface(surface)
    cr.paint()

如果这正是您想要的,我建议您更新问题的标题,以反映您的实际需要:)。

谢谢。我已经浏览了PyGTK-Pixbuf文档,但是似乎没有任何方法可以将它们用作Cairo可以利用的曲面。我是否需要在Pixbuf和ImageSurface之间来回转换才能使用它们?另请参见