[pygtk] Pb drawing on a pixmap
John Finlay
finlay at moeraki.com
Wed Mar 29 03:40:49 WST 2006
Elby wrote:
> Hi,
>
> I'm trying to draw on a pixmap, but all I get is a black window.
> It seems I've got a problem with Graphic Context, but I don't understand why.
> Here is a simple test :
>
> - I create a gdk.Color
> - I create a pixmap and a new Graphic Context with background and foreground color equal to this color :
>
>
> WIDTH = 200
> HEIGHT = 200
> color_name = 'navajowhite'
> gdk_color = gtk.gdk.color_parse(color_name)
> pixmap2 = gtk.gdk.Pixmap(None, WIDTH, HEIGHT, 24)
> gc = gtk.gdk.GC(pixmap2,background=gdk_color, foreground=gdk_color)
>
> - when I compare my gdk.Color with gc.foreground and gc.background I get different colors :
>
> print "%8s "*4 % ('pixel', 'red','green','blue')
> for x in (gdk_color, gc.background, gc.foreground) :
> print "%8d "*4 % (x.pixel, x.red, x.green, x.blue)
>
> pixel red green blue
> 0 65535 57054 44461
> 0 19164 47096 20704
> 0 1 0 0
>
> Why ?
> I made a few test with pixmap2.new_gc(...) but I didn't have more success
> Can you explain me how to set a graphic context correctly and how to draw on a pixmap ?
>
> Attached is the code I use to test drawing on a pixmap.
> Have you any idea to make it work correctly ?
>
You need to allocate the color to use it in the GC.
gtk.gdk.color_parse() does not allocate the color. See mods below.
> Best regards
>
> -- Elby
>
>
> ------------------------------------------------------------------------
>
> #!/bin/python
> # -*- coding: iso-8859-15 -*-
>
> import pygtk
> pygtk.require("2.0")
> import gtk
>
> WIDTH = 200
> HEIGHT = 200
>
> # Main window
> window = gtk.Window(gtk.WINDOW_TOPLEVEL)
> window.set_default_size(2*WIDTH,HEIGHT)
> window.connect("destroy", lambda w: gtk.main_quit())
> window.show()
> hbox = gtk.HBox()
> hbox.show()
> window.add(hbox)
>
> # Pixmap tests
> color_name = 'navajowhite'
> gdk_color = gtk.gdk.color_parse(color_name)
>
colormap = window.window.get_colormap()
gdk_color = colormap.alloc_color(color_name)
> # pixmap 1 : from xpm
> # Creating xpm data
> xpm_data = [
> "%d %d 1 1" % (WIDTH, HEIGHT),
> " c %s" % color_name,
> ] + [" "*WIDTH]*HEIGHT
>
> pixmap1, mask1 = gtk.gdk.pixmap_create_from_xpm_d(window.window, None, xpm_data)
> image1 = gtk.Image()
> image1.set_from_pixmap(pixmap1, mask1)
> image1.show()
> hbox.pack_start(image1)
>
> # pixmap 2 : using gtk.gdk.Drawable methods
> pixmap2 = gtk.gdk.Pixmap(None, WIDTH, HEIGHT, 24)
> gc = gtk.gdk.GC(pixmap2,background=gdk_color, foreground=gdk_color)
> gc.set_background(gdk_color)
>
> # Test gc background and foreground
> print "%8s "*4 % ('pixel', 'red','green','blue')
> for x in (gdk_color, gc.background, gc.foreground) :
> print "%8d "*4 % (x.pixel, x.red, x.green, x.blue)
> #
> # WHY are these colors different ?
> #
>
> pixmap2.draw_rectangle(gc, True, 0, 0, WIDTH, HEIGHT)
> image2 = gtk.Image()
> image2.set_from_pixmap(pixmap2, None)
> image2.show()
> hbox.add(image2)
>
> gtk.main()
>
More information about the pygtk
mailing list