[pygtk] Multiple Gtk.DrawingArea
John Finlay
finlay at moeraki.com
Thu Sep 10 11:01:10 WST 2009
Antoine Cailliau wrote:
> Dear all,
>
> First of all, a short presentation since I'm new on the ML. I'm student
> in Belgium (UCLouvain) in Computer Science (especially computer langages
> and software engineering). I just started a small project (a game for
> gnome) to get familiar with Cairo and Pango libs.
>
> I need two distinct drawing area in a gtk window. I tried a naive way
> but I only get one working area (the first is accordingly filled with a
> black rectangle, the other seems to be not displayed). I presume this is
> not the right way to do it.
>
> Here is the code:
>
> #!/usr/bin/env python
> #
>
> import gtk, cairo
>
> class DA(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()
>
> # set a clip region for the expose event
> self.context.rectangle(event.area.x, event.area.y,
> event.area.width, event.area.height)
> self.context.clip()
>
> self.draw(self.context)
>
> return False
>
> def draw(self, context):
> rect = self.get_allocation()
>
don't use this since it returns the allocation within the Window not
within the DA. Use self.window.get_size() to get the width and height
and use 0, 0 for the x and y in the following.
> context.rectangle(rect.x, rect.y, rect.width, rect.height)
> context.fill()
> return False
>
> def main():
> window = gtk.Window()
> da1 = DA()
> da2 = DA()
>
> vbox = gtk.VBox()
> vbox.add(da1)
> vbox.add(da2)
> window.add(vbox)
>
> window.connect("destroy", gtk.main_quit)
> window.show_all()
> gtk.main()
>
> if __name__ == "__main__":
> main()
>
>
More information about the pygtk
mailing list