[pygtk] DrawingArea in pygtk2

John Finlay finlay@moeraki.com
Sat, 26 Oct 2002 23:01:38 -0700

I'm in the process of upgrading the tutorial to GTK+ 2.0. It looks like 
you have to draw on a drawable which is a pixmap or window. A 
drawingarea has a window accessed as drawingarea.window.

Your rewritten test case is:

#!/usr/bin/env python2

# Draw a blue rectangle on DrawingArea, that's all

import gtk
import string

def area_expose_cb(area, event):
    global gc

    gc = area.get_style().fg_gc[gtk.STATE_NORMAL]
    gc.foreground = blue
    area.window.draw_rectangle(gc, gtk.TRUE, 50, 50, 100, 50)

if __name__ == "__main__":
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_title("drawing test example")
    window.connect("destroy", gtk.mainquit)
    area = gtk.DrawingArea()
    width = 400; hight = 300
    area.set_size_request(width, hight)
    colormap = area.get_colormap()
    blue = colormap.alloc_color(0, 0, 65535)
    area.connect("expose-event", area_expose_cb)



Viktor Hornak wrote:

> Hello,
> I am new to pygtk but was very excited to find out about it. It seemed
> like the easiest way to get started with some simple graphics programs. 
> I found an excellent tutorial (with example for DrawingArea) at:
> http://www.moeraki.com/pygtktutorial/sec-drawingareamethods.html
> The DrawingArea example given on the page would be all that I need right
> now to get started. Unfortunately, when I upgraded my RedHat7.3
> (python1.5, gtk+) to 8.0 (python2.2, gtk2+) things weren't working
> according to tutorial any more. That's quite understandable, but they
> don't seem to work even after I rewrote things according to FAQ-2.3
> (migrating from PyGTK-0 to PyGTK-2). All I need is to get the basic
> DrawingArea methods to work in PyGTK2. Here is how I rewrote a simple
> test example (based on the above given tutorial):