There are a general set of methods that can be used to draw onto the drawing area 'canvas'. These drawing methods work for any gtk.gdk.Drawable which is a gtk.gdk.Window or a gtk.gdk.Pixmap. The drawing methods are:
drawable.draw_point(gc, x, y) |
gc is the Graphics Context to be used to do the drawing.
x and y are the coordinates of the point.
drawable.draw_line(gc, x1, y1, x2, y2) |
gc is the Graphics Context.
x1 and y1 specify the starting point of the line. x2 and y2 specify the ending point of the line.
drawable.draw_rectangle(gc, filled, x, y, width, height) |
where gc is the Graphics Context.
filled is a boolean indicating the rectangle should be filled with the foreground color if TRUE or not filled, if FALSE.
x and y are the top left corner of the rectangle.
width and height are the width and height of the rectangle.
drawable.draw_arc(gc, filled, x, y, width, height, angle1, angle2) |
gc is the Graphics Context.
filled is a boolean indicating the arc should be filled with the foreground color if TRUE or not filled, if FALSE.
x and y are the top left corner of the bounding rectangle. width and height are the width and height of the bounding rectangle.
angle1 is the start angle of the arc, relative to the 3 o'clock position, counter-clockwise, in 1/64ths of a degree.
angle2 is the end angle of the arc, relative to angle1, in 1/64ths of a degree counter clockwise.
drawable.draw_polygon(gc, filled, points) |
gc is the Graphics Context.
filled is a boolean indicating the polygon should be filled with the foreground color if TRUE or not filled, if FALSE.
points is a list of coordinate pairs in tuples e.g. [ (0,0), (2,5), (3,7), (4,11) ] of the points to be drawn as a connected polygon.
drawable.draw_string(font, gc, x, y, string) drawable.draw_text(font, gc, x, y, string) |
font is the gtk.gdk.Font to use to render the string.
gc is the Graphics Context.
x and y are the coordinates of the point to start rendering the string i.e the left baseline.
string is the string of characters to render.
Both the draw_string() and draw_text() methods are deprecated - use a pango.Layout instead with the draw_layout() method.
drawable.draw_layout(gc, x, y, layout) |
gc is the Graphics Context.
x and y are the coordinates of the point to start rendering the layout.
layout is the pango.Layout that is to be rendered.
drawable.draw_drawable(gc, src, xsrc, ysrc, xdest, ydest, width, height) |
gc is the Graphics Context.
src is the source drawable.
xsrc and ysrc are the coordinates of the top left rectangle in the source drawable.
xdest and ydest are the coordinates of the top left corner in the drawing area.
width and height are the width and height of the source drawable area to be copied to the drawable. If width or height is -1 then the full width or height of the drawable is used.
drawable.draw_image(gc, image, xsrc, ysrc, xdest, ydest, width, height) |
gc is the Graphics Context.
image is the source image.
xsrc and ysrc are the coordinates of the top left rectangle in the source drawable.
xdest and ydest are the coordinates of the top left corner in the drawing area.
width and height are the width and height of the source drawable area to be copied to the drawable. If width or height is -1 then the full width or height of the image is used.
drawable.draw_points(gc, points) |
gc is the Graphics Context.
points is a list or tuple of coordinate pairs in tuples e.g. [ (0,0), (2,5), (3,7), (4,11) ] of the points to be drawn.
drawable.draw_segments(gc, segs) |
gc is the Graphics Context.
segs is a list or tuple of start and end coordinate pairs in tuples e.g. [ (0,0, 1,5), (2,5, 1,7), (3,7, 1,11), (4,11, 1,13) ] of the line segments to be drawn.
drawable.draw_lines(gc, points) |
gc is the Graphics Context.
points is a list or tuple of coordinate pairs in tuples e.g. [ (0,0), (2,5), (3,7), (4,11) ] of the points to be connected with lines.
drawable.draw_rgb_image(gc, x, y, width, height, dith, rgb_buf, rowstride) drawable.draw_rgb_32_image(gc, x, y, width, height, dith, buf, rowstride) drawable.draw_gray_image(gc, x, y, width, height, dith, buf, rowstride) |
gc is the Graphics Context.
x and y are the top left corner of the image bounding rectangle.
width and height are the width and height of the image bounding rectangle.
dith is the dither mode as described below
For the draw_rgb_image() method, rgb_buf is the RGB Image data packed in a string as a sequence of 8-bit RGB pixel triplets. For the draw_rgb_32_image() method, buf is the RGB Image data packed in a string as a sequence of 8-bit RGB pixel triplets with 8-bit padding (4 characters per RGB pixel). For the draw_gray_image() method, buf is the gray image data packed in a string as 8-bit pixel data.
rowstride is the number of characters from the start of one row to the start of the next row of the image. rowstride usually defaults to: 3 * width for the draw_rgb_image() method; 4 * width for the draw_rgb_32_image(); and, width for the draw_gray_image() method. If rowstride is 0 the line will be replicated height times.
The dither modes are:
RGB_DITHER_NONE # Never use dithering. RGB_DITHER_NORMAL # Use dithering in 8 bits per pixel (and below) only. RGB_DITHER_MAX # Use dithering in 16 bits per pixel and below. |
The drawingarea.py example program demonstrates the use of most of the DrawingArea methods. Figure 12.1 shows the resulting display:
The drawingarea.py source code is below and uses the gtk.xpm pixmap:
1 #!/usr/bin/env python 2 3 # example drawingarea.py 4 5 import pygtk 6 pygtk.require('2.0') 7 import gtk 8 import operator 9 import time 10 import string 11 12 class DrawingAreaExample: 13 def __init__(self): 14 window = gtk.Window(gtk.WINDOW_TOPLEVEL) 15 window.set_title("Drawing Area Example") 16 window.connect("destroy", lambda w: gtk.main_quit()) 17 self.area = gtk.DrawingArea() 18 self.area.set_size_request(400, 300) 19 self.pangolayout = self.area.create_pango_layout("") 20 window.add(self.area) 21 self.area.connect("expose-event", self.area_expose_cb) 22 self.area.show() 23 window.show() 24 25 def area_expose_cb(self, area, event): 26 self.style = self.area.get_style() 27 self.gc = self.style.fg_gc[gtk.STATE_NORMAL] 28 self.draw_point(10,10) 29 self.draw_points(110, 10) 30 self.draw_line(210, 10) 31 self.draw_lines(310, 10) 32 self.draw_segments(10, 100) 33 self.draw_rectangles(110, 100) 34 self.draw_arcs(210, 100) 35 self.draw_pixmap(310, 100) 36 self.draw_polygon(10, 200) 37 self.draw_rgb_image(110, 200) 38 return gtk.TRUE 39 40 def draw_point(self, x, y): 41 self.area.window.draw_point(self.gc, x+30, y+30) 42 self.pangolayout.set_text("Point") 43 self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout) 44 return 45 46 def draw_points(self, x, y): 47 points = [(x+10,y+10), (x+10,y), (x+40,y+30), 48 (x+30,y+10), (x+50,y+10)] 49 self.area.window.draw_points(self.gc, points) 50 self.pangolayout.set_text("Points") 51 self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout) 52 return 53 54 def draw_line(self, x, y): 55 self.area.window.draw_line(self.gc, x+10, y+10, x+20, y+30) 56 self.pangolayout.set_text("Line") 57 self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout) 58 return 59 60 def draw_lines(self, x, y): 61 points = [(x+10,y+10), (x+10,y), (x+40,y+30), 62 (x+30,y+10), (x+50,y+10)] 63 self.area.window.draw_lines(self.gc, points) 64 self.pangolayout.set_text("Lines") 65 self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout) 66 return 67 68 def draw_segments(self, x, y): 69 segments = ((x+20,y+10, x+20,y+70), (x+60,y+10, x+60,y+70), 70 (x+10,y+30 , x+70,y+30), (x+10, y+50 , x+70, y+50)) 71 self.area.window.draw_segments(self.gc, segments) 72 self.pangolayout.set_text("Segments") 73 self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout) 74 return 75 76 def draw_rectangles(self, x, y): 77 self.area.window.draw_rectangle(self.gc, gtk.FALSE, x, y, 80, 70) 78 self.area.window.draw_rectangle(self.gc, gtk.TRUE, x+10, y+10, 20, 20) 79 self.area.window.draw_rectangle(self.gc, gtk.TRUE, x+50, y+10, 20, 20) 80 self.area.window.draw_rectangle(self.gc, gtk.TRUE, x+20, y+50, 40, 10) 81 self.pangolayout.set_text("Rectangles") 82 self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout) 83 return 84 85 def draw_arcs(self, x, y): 86 self.area.window.draw_arc(self.gc, gtk.FALSE, x+10, y, 70, 70, 87 0, 360*64) 88 self.area.window.draw_arc(self.gc, gtk.TRUE, x+30, y+20, 10, 10, 89 0, 360*64) 90 self.area.window.draw_arc(self.gc, gtk.TRUE, x+50, y+20, 10, 10, 91 0, 360*64) 92 self.area.window.draw_arc(self.gc, gtk.TRUE, x+30, y+10, 30, 50, 93 210*64, 120*64) 94 self.pangolayout.set_text("Arcs") 95 self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout) 96 return 97 98 def draw_pixmap(self, x, y): 99 pixmap, mask = gtk.gdk.pixmap_create_from_xpm( 100 self.area.window, self.style.bg[gtk.STATE_NORMAL], "gtk.xpm") 101 102 self.area.window.draw_drawable(self.gc, pixmap, 0, 0, x+15, y+25, 103 -1, -1) 104 self.pangolayout.set_text("Pixmap") 105 self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout) 106 return 107 108 def draw_polygon(self, x, y): 109 points = [(x+10,y+60), (x+10,y+20), (x+40,y+70), 110 (x+30,y+30), (x+50,y+40)] 111 self.area.window.draw_polygon(self.gc, gtk.TRUE, points) 112 self.pangolayout.set_text("Polygon") 113 self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout) 114 return 115 116 def draw_rgb_image(self, x, y): 117 b = 80*3*80*['\0'] 118 for i in range(80): 119 for j in range(80): 120 b[3*80*i+3*j] = chr(255-3*i) 121 b[3*80*i+3*j+1] = chr(255-3*abs(i-j)) 122 b[3*80*i+3*j+2] = chr(255-3*j) 123 buff = string.join(b, '') 124 self.area.window.draw_rgb_image(self.gc, x, y, 80, 80, 125 gtk.gdk.RGB_DITHER_NONE, buff, 80*3) 126 self.pangolayout.set_text("RGB Image") 127 self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout) 128 return 129 130 def main(): 131 gtk.main() 132 return 0 133 134 if __name__ == "__main__": 135 DrawingAreaExample() 136 main() |