[pygtk] newbie drawingarea question..

Rory Walsh rorywalsh at ear.ie
Sat Oct 13 01:15:35 WST 2007


I'm trying to animate the drawing of a line using drawingarea but I'm 
having some difficulty. I thought that I could just call draw_point() 
and increment the X coordinate each time but my old points are not 
staying on the screen? My code is below, any ideas?

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import math

def redraw_timeout(object):
     object.window.invalidate_rect((0,0,200,200),False)
     return True
	
class drawSomething:
	x = 0
	def on_expose(self, drawingarea, event):
	    # get the GdkWindow
	    self.window = event.window
	    # get the GC
	    gc = self.window.new_gc()
	    # draw a line?
	    self.drawingarea.window.draw_point(gc, self.x, 100)
	    self.x = self.x+1
	    if(self.x>200):
	    	self.x = 0

	def __init__(self):
		self.win = gtk.Window()
		self.win.connect("destroy", gtk.mainquit)
		self.win.show()
		self.drawingarea = gtk.DrawingArea()
		self.drawingarea.connect("expose-event", self.on_expose)
		self.drawingarea.show()
		self.win.add(self.drawingarea)
		
	def main(self):
		self.drawingarea.show()
		gtk.main()


		
# enter the mainloop
if __name__ == "__main__":
     draw = drawSomething()
     source_id = gobject.timeout_add(1, redraw_timeout, draw)
     draw.main()

regards,
Rory.


More information about the pygtk mailing list