pygtk: Draw lines onto a gtk.gdk.Pixbuf

不羁的心 提交于 2019-12-04 12:58:40

I was doing something similar (drawing to a gdk.Drawable), and I found that set_foreground doesn't work. To actually draw using the color I wanted, I used the following:

# Red!
gc.set_rgb_fg_color(gtk.gdk.Color(0xff, 0x0, 0x0))

Oh my god. So painful. Here it is:

    w,h = pixbuf.get_width(), pixbuf.get_height()
    drawable = gtk.gdk.Pixmap(None, w, h, 24)
    gc = drawable.new_gc()
    drawable.draw_pixbuf(gc, pixbuf, 0, 0, 0, 0, -1, -1)

    #---ACTUAL DRAWING CODE---
    gc.set_foreground(gtk.gdk.Color(65535, 0, 0))
    drawable.draw_line(gc, 0, 0, w, h)
    #-------------------------

    cmap = gtk.gdk.Colormap(gtk.gdk.visual_get_best(), False)
    pixbuf.get_from_drawable(drawable, cmap, 0, 0, 0, 0, w, h)

It actually draws a black line ATM, not a red one, so I have some work left to do...

Have you tried cairo context? sorry i seem cant comment on your post. hence i posted it here.

I haven't tried this myself but I believe that cairo is your friend when it comes to drawing in gtk. you can set the source of your cairo context as pixbuf so i think this is usable for you.

gdkcairocontext

image = gtk.Image()
pixmap,mask = pixbuf.render_pixmap_and_mask() # Function call
cm = pixmap.get_colormap()
red = cm.alloc_color('red')
gc = pixmap.new_gc(foreground=red)
pixmap.draw_line(gc,0,0,w,h)
image.set_from_pixmap(pixmap,mask)

Should do the trick.

You should connect to the expose-event (GTK 2) or draw (GTK 3) signal. In that handler, simply use image.window to get the widget's gtk.gdk.Window; this is a subclass of gtk.gdk.Drawable, so you can draw on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!