#!/usr/bin/env python ### CSC15, Hofstra University - Professor Chuck Liang #### Most of the first part of the program is to be left alone: be sure #### to save a copy before you change something. import pygtk import gtk width = 800 # width and height of window in pixels height = 650 window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("Simplified Drawing Example") window.connect("destroy", lambda w: gtk.main_quit()) area = gtk.DrawingArea() area.set_size_request(width,height) window.resize(width,height) window.add(area) def area_expose_cb(area, event): global mydraw mydraw(area.window) area.connect("expose-event", area_expose_cb) window.show_all() window.move(0,0) ## gc is called a "graphical context": main object used to draw with gc = area.window.new_gc() cmap = gc.get_colormap() ### Some convenient colors, others can be defined using RGB values as shown: green = cmap.alloc_color(red=0,green=65535,blue=0) blue = cmap.alloc_color(red=0,green=0,blue=65535) red = cmap.alloc_color(red=65535,green=0,blue=0) white = cmap.alloc_color("white") black = cmap.alloc_color("black") gray = cmap.alloc_color("gray") yellow = cmap.alloc_color("yellow") purple = cmap.alloc_color("purple") gc.set_foreground(green) gc.set_background(blue) gc.line_width = 1 # thickness of lines # convenient function to draw a circle using built-in draw_arc function. def drawcircle(brush,x,y,radius,fill): brush.draw_arc(gc,fill,x-radius,y-radius,2*radius,2*radius,0,360*64) ## For detailed documentation on drawing methods, see: # https://developer.gnome.org/pygtk/stable/class-gdkdrawable.html ###################### Your code starts here ######################### def mydraw(brush): gc.set_foreground(white) # clear background ... brush.draw_rectangle(gc,True,0,0,width,height) # with a solid rectangle gc.set_foreground(blue) # sets drawing color brush.draw_line(gc,10,20,width-10,height-10) brush.draw_rectangle(gc,False,40,40,200,60) gc.set_foreground(red) # drawing a circle is a little different from drawing other shapes: drawcircle(brush,width/2,height/2,100,False) drawcircle(brush,width/2,height/2,50,True) # end mydraw # The following line must be the last line in the program: gtk.main()