I'm using PyGTK and I want to draw something inside a CellRenderer, so I'm using a CellRendererPixbuff, and it's working. The only problem is that if that drawing has transparency, the output will be bad and mixed up.
Screenshot: http://postimage.org/image/f839ijd0/
My code:
def pixbufSqFromColor(color, size, roundR=0):## a rounded square with specified color
pmap = gdk.Pixmap(None, size, size, depth=24)
cr = pmap.cairo_create()
###
#cr.rectangle(0, 0, size, size)
#fillColor(cr, (255, 255, 255, 0))
###
cr.move_to(roundR, 0)
cr.line_to(size-roundR, 0)
cr.arc(size-roundR, roundR, roundR, 3*pi/2, 2*pi) ## up right corner
cr.line_to(size, size-roundR)
cr.arc(size-roundR, size-roundR, roundR, 0, pi/2) ## down right corner
cr.line_to(roundR, size)
cr.arc(roundR, size-roundR, roundR, pi/2, pi) ## down left corner
cr.line_to(0, roundR)
cr.arc(roundR, roundR, roundR,开发者_如何学C pi, 3*pi/2) ## up left corner
###
cr.close_path()
fillColor(cr, color)
####
pbuf = gdk.Pixbuf(gdk.COLORSPACE_RGB, True, 8, size, size)
colormap = gtk.gdk.colormap_get_system()
#colormap = self.get_screen().get_system_colormap()
#colormap = pmap.get_colormap()
pbuf.get_from_drawable(pmap, colormap, 0, 0, 0, 0, size, size)
return pbuf
#return gdk.Pixbuf.get_from_drawable(pmap, gdk.COLORSPACE_RGB, 0, 0, 0, 0, size, size)
And this is where that function is used
groupNode = self.treestore.append((
-1,
group.enable,
pixbufSqFromColor(group.color, 50, 15),
group.title,
'',
))
And some used functions here:
def setColor(cr, color):
## arguments to set_source_rgb and set_source_rgba must be between 0 and 1
if len(color)==3:
cr.set_source_rgb(color[0] / 255.0,
color[1] / 255.0,
color[2] / 255.0)
elif len(color)==4:
cr.set_source_rgba(color[0] / 255.0,
color[1] / 255.0,
color[2] / 255.0,
color[3] / 255.0)
else:
raise ValueError('bad color %s'%str(color))
def fillColor(cr, color):
setColor(cr, color)
cr.fill()
I've a similar problem and it seems to stem from gdk.Pixmap
but I have no solution yet.
Edit :
I've solved my problem by using gtk.gdk.pixbuf_new_from_xpm_data()
to generate the pixbuf, instead of gdk.Pixmap
and Cairo operations. I know you can't use it because you have size
and roundR
parameters.
Maybe you can fill the gdk.Pixmap
with a color that you'll replace with gtk.gdk.Pixbuf.add_alpha()
when the pixbuf will be generated.
精彩评论