Hi,
I cannot understand why the program below, is not able to update the
draw when I push the button.
I.e. when the button is pushed the code in Plot::calc is executed.
At the end of this code I must emit a expose-event but the call
to self.queue_draw_area is not working at all..
help, please.
Thanks,
A. Caruso
from numpy import *
import pysmm_graphlib as gl
import numpy.random
import framework
from math import pi
n = 50
x = numpy.random.random(n)
y = numpy.random.random(n)
(ll, hh, dist) = gl.PYSMMGL_Emst(x,y)
triangles = gl.PYSMMGL_Delaunay(x,y)
class Plot(framework.Screen):
def calc(self):
x = numpy.random.random(n)
y = numpy.random.random(n)
(ll, hh, dist) = gl.PYSMMGL_Emst(x,y)
triangles = gl.PYSMMGL_Delaunay(x,y)
(w,h) = self.window.get_size()
self.queue_draw_area(0,0,w,h)
def draw(self, cr, width, height):
# draw the background
cr.set_source_rgb(1,1,1)
cr.rectangle(0,0,width,height)
cr.fill()
# draw points
cr.set_source_rgb(0,0,0)
for i in xrange(len(x)):
cr.arc(x[i]*width,y[i]*height,2,0,2*pi)
cr.fill()
# draw the triangulation
cr.set_source_rgb(0,0,0.5)
cr.set_line_width(0.3)
for t in triangles:
cr.move_to(x[t[0]] *width,y[t[0]]*height)
cr.line_to(x[t[1]] *width,y[t[1]]*height)
cr.line_to(x[t[2]] *width,y[t[2]]*height)
cr.line_to(x[t[0]] *width,y[t[0]]*height)
cr.stroke()
cr.set_source_rgb(1,0,0)
cr.set_line_width(2)
for i in xrange(len(ll)):
cr.move_to(x[ll[i]]*width, y[ll[i]]*height)
cr.line_to(x[hh[i]]*width, y[hh[i]]*height)
cr.stroke()
framework.run(Plot)
#! /usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk, gobject, cairo
# Create a GTK+ widget on which we will draw using Cairo
class Screen(gtk.DrawingArea):
# Draw in response to an expose-event
__gsignals__ = { "expose-event": "override" }
# Handle the expose-event by drawing
def do_expose_event(self, event):
# Create the cairo context
cr = self.window.cairo_create()
# Restrict Cairo to the exposed area; avoid extra work
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
self.draw(cr, *self.window.get_size())
def calc(self):
print "Redefine this method in the Derived class"
def draw(self, cr, width, height):
# Fill the background with gray
cr.set_source_rgb(0.5, 0.5, 0.5)
cr.rectangle(0, 0, width, height)
cr.fill()
# GTK mumbo-jumbo to show the widget in a window and quit when it's closed
def run(Widget):
window = gtk.Window()
window.connect("delete-event", gtk.main_quit)
vbox = gtk.VBox(False,2)
widget = Widget()
widget.set_size_request(500,500)
vbox.pack_start(widget,False,False,0)
widget.show()
button = gtk.Button("Recalc")
vbox.pack_start(button,False,False,0)
button.connect("clicked", (lambda s,w: w.calc()), widget)
button.show()
vbox.show()
window.add(vbox)
window.present()
gtk.main()
if __name__ == "__main__":
run(Screen)
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/