{"id":360,"date":"2012-10-15T16:32:53","date_gmt":"2012-10-15T21:32:53","guid":{"rendered":"http:\/\/www.virtualroadside.com\/blog\/?p=360"},"modified":"2012-10-15T16:32:53","modified_gmt":"2012-10-15T21:32:53","slug":"drawing-in-color-in-pygtk","status":"publish","type":"post","link":"http:\/\/www.virtualroadside.com\/blog\/index.php\/2012\/10\/15\/drawing-in-color-in-pygtk\/","title":{"rendered":"Drawing in color in PyGTK"},"content":{"rendered":"<p>I&#8217;ve been playing with drawing on your own widgets in PyGTK on Windows, and I found it incredibly difficult to figure out how to draw something in color on a gtk.gdk.Drawable object using draw_line, draw_rectangle, etc. You can&#8217;t just set the color using the semi-obvious mechanism:<\/p>\n<pre>\r\n    gc = widget.window.new_gc()\r\n    gc.set_foreground(gtk.gdk.Color(255,0,0))\r\n<\/pre>\n<p>I think the reason it doesn&#8217;t work is because if the color isn&#8217;t in the device-specific colormap, then GTK will ignore whatever color you set without bothering to warn you that something is wrong. However, I&#8217;ve finally hit on something that works. In your expose event (or elsewhere), you can put in something like the following:<\/p>\n<pre>    def on_expose_event(self, widget, event):\r\n\r\n        gc = widget.window.new_gc()\r\n        colormap = self.gc.get_colormap()\r\n        color = colormap.alloc_color('yellow')\r\n        gc.set_foreground(color)\r\n\r\n        # whatever gtk.gdk.Drawable draw_* functions you call here\r\n        # will use that color<\/pre>\n<p>Hope you find this useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been playing with drawing on your own widgets in PyGTK on Windows, and I found it incredibly difficult to figure out how to draw something in color on a gtk.gdk.Drawable object using draw_line, draw_rectangle, etc. You can&#8217;t just set the color using the semi-obvious mechanism: gc = widget.window.new_gc() gc.set_foreground(gtk.gdk.Color(255,0,0)) I think the reason it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,45,47],"tags":[],"_links":{"self":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/360"}],"collection":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":2,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":362,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions\/362"}],"wp:attachment":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}