[pygtk] Using draw_rectangle with a fill colour

Timo timomlists at gmail.com
Wed Mar 2 00:56:37 WST 2011


On 01-03-11 15:20, mercado wrote:
> Hello all,
>
> I am trying to use gtk.DrawingArea to draw an orange rectangle, using
> some of the information at
> http://www.pygtk.org/pygtk2tutorial/ch-DrawingArea.html.
>
> I have included a sample of my code below, which draws a black
> rectangle.  However, I cannot get it to accept the color information
> from gc.
>
> Can anyone tell me what I'm doing wrong?
>
> Thanks in advance.
>
> --------------------------------------------------------------------------------
>
> import gtk
>
> class CustomDrawingArea(gtk.DrawingArea):
>      def __init__(self):
>          gtk.DrawingArea.__init__(self)
>          self.connect("expose_event", self.expose)
>
>      def expose(self, widget, event):
>          self.context = widget.window.cairo_create()
>          self.draw(self.context)
>
>          return False
>
>      def draw(self, context):
>          drawable = self.window
>          gc = drawable.new_gc()
>
>          gc.foreground = gtk.gdk.color_parse("orange")
Try the following line instead:
         gc.set_foreground(self.get_colormap().alloc_color("#FF8000"))

Cheers,
Timo

>          gc.fill = gtk.gdk.SOLID
>
>          drawable.draw_rectangle(gc, True, x=100, y=100, width=10, height=50)
>
> def main():
>      window = gtk.Window()
>      window.set_size_request(500, 500)
>
>      cda = CustomDrawingArea()
>
>      window.add(cda)
>      window.connect("destroy", gtk.main_quit)
>      window.show_all()
>
>      gtk.main()
>
> if __name__ == "__main__":
>      main()
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/



More information about the pygtk mailing list