Dear colleagues
I am having difficulty understanding how to alter the
colour of lines I am drawing, and would be grateful
for some assistance.
I have attached the python code which I have altered
from the example.
Regards
Jim Leven
--
Email : [EMAIL PROTECTED]
#!/usr/bin/env python
# gen_poly.py
import pygtk
pygtk.require('2.0')
import gtk
import operator
import time
import string
import read_segs
global horizons
class Drawing_Segs:
def __init__( self, min_X, max_X, min_Z, max_Z ):
self.x_win_min = 0
self.x_win_max = 1000
self.y_win_min = 0
self.y_win_max = 800
self.min_X = min_X
self.max_X = max_X
self.min_Z = min_Z
self.max_Z = max_Z
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Generate Polygons")
window.connect("destroy", lambda w: gtk.main_quit())
self.area = gtk.DrawingArea()
self.area.set_size_request( self.x_win_max, self.y_win_max )
self.pangolayout = self.area.create_pango_layout("")
self.sw = gtk.ScrolledWindow()
self.sw.add_with_viewport(self.area)
self.table = gtk.Table(2,2)
self.hruler = gtk.HRuler()
self.vruler = gtk.VRuler()
self.hruler.set_range( self.x_win_min, self.x_win_max, self.x_win_min, self.x_win_max )
self.vruler.set_range( self.y_win_min, self.y_win_max, self.y_win_min, self.y_win_max )
self.table.attach(self.hruler, 1, 2, 0, 1, yoptions=0)
self.table.attach(self.vruler, 0, 1, 1, 2, xoptions=0)
self.table.attach(self.sw, 1, 2, 1, 2)
window.add(self.table)
self.area.set_events(gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.POINTER_MOTION_HINT_MASK )
self.area.connect("expose-event", self.area_expose_cb)
def motion_notify(ruler, event):
return ruler.emit("motion_notify_event", event)
self.area.connect_object("motion_notify_event", motion_notify,
self.hruler)
self.area.connect_object("motion_notify_event", motion_notify,
self.vruler)
self.hadj = self.sw.get_hadjustment()
self.vadj = self.sw.get_vadjustment()
def val_cb(adj, ruler, horiz):
if horiz:
span = self.sw.get_allocation()[3]
else:
span = self.sw.get_allocation()[2]
l,u,p,m = ruler.get_range()
v = adj.value
ruler.set_range(v, v+span, p, m)
while gtk.events_pending():
gtk.main_iteration()
self.hadj.connect('value-changed', val_cb, self.hruler, True)
self.vadj.connect('value-changed', val_cb, self.vruler, False)
def size_allocate_cb(wid, allocation):
x, y, w, h = allocation
l,u,p,m = self.hruler.get_range()
m = max(m, w)
self.hruler.set_range(l, l+w, p, m)
l,u,p,m = self.vruler.get_range()
m = max(m, h)
self.vruler.set_range(l, l+h, p, m)
self.sw.connect('size-allocate', size_allocate_cb)
self.area.show()
self.hruler.show()
self.vruler.show()
self.sw.show()
self.table.show()
window.show()
def area_expose_cb(self, area, event):
self.style = self.area.get_style()
self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
keys = horizons.keys()
keys.sort()
for key in keys :
for segment in horizons[key] :
for node in segment :
self.draw_point( node[0], node[1] )
# color = gtk.gdk.Color(red=65535, green=0, blue=0, pixel=0)
# self.gc.set_foreground(color)
# color = cmap.alloc(49*255,79*255,79*255) # slate grey
# cmap = self.get_colormap()
# color = cmap.alloc(0,0,65535) # blue
# gc = widget.get_parent_window().new_gc()
# gc.foreground = color
# fill = 1
# x = y = 400
# r1 = r2 = 30
# widget.draw_arc(gc, fill, x, y, r1, r2, 0, 360*64)
# self.gc = self.get_parent_window().new_gc()
# color = gtk.gdk.color_parse("red")
# gtk.gdk.GC.set_foreground( color )
# self.gc.foreground = color
self.style = self.area.get_style().white_gc
self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
for key in keys :
for segment in horizons[key] :
# pass
self.draw_lines( segment )
# self.area.modify_fg(gtk.STATE_NORMAL, color )
# self.draw_points(110, 10)
# self.draw_line(210, 10)
# self.draw_lines(310, 10)
# self.draw_segments(10, 100)
# self.draw_rectangles(110, 100)
# self.draw_arcs(210, 100)
# self.draw_pixmap(310, 100)
# self.draw_polygon(10, 200)
# self.draw_rgb_image(110, 200)
return True
def draw_point(self, xx, yy):
x = self.x_win_min + (self.x_win_max - self.x_win_min) * (xx - self.min_X) / \
(self.max_X - self.min_X)
y = self.y_win_min + (self.y_win_max - self.y_win_min) * (yy - self.min_Z) / \
(self.max_Z - self.min_Z)
self.area.window.draw_point(self.gc, x, y)
# self.pangolayout.set_text("Point")
# self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout)
return
# def draw_points(self, x, y):
# points = [(x+10,y+10), (x+10,y), (x+40,y+30),
# (x+30,y+10), (x+50,y+10)]
# self.area.window.draw_points(self.gc, points)
# # self.pangolayout.set_text("Points")
# # self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout)
# return
# def draw_line(self, x, y):
# self.area.window.draw_line(self.gc, x+10, y+10, x+20, y+30)
# # self.pangolayout.set_text("Line")
# # self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout)
# return
def draw_lines( self, list_of_nodes ):
points = []
for node in list_of_nodes :
(xx, yy) = node
x = self.x_win_min + (self.x_win_max - self.x_win_min) * (xx - self.min_X) / \
(self.max_X - self.min_X)
y = self.y_win_min + (self.y_win_max - self.y_win_min) * (yy - self.min_Z) / \
(self.max_Z - self.min_Z)
points.append( (x,y) )
self.area.window.draw_lines(self.gc, points)
# self.pangolayout.set_text("Lines")
# self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout)
return
# def draw_lines(self, x, y):
# points = [(x+10,y+10), (x+10,y), (x+40,y+30),
# (x+30,y+10), (x+50,y+10)]
# self.area.window.draw_lines(self.gc, points)
# # self.pangolayout.set_text("Lines")
# # self.area.window.draw_layout(self.gc, x+5, y+50, self.pangolayout)
# return
# def draw_segments(self, x, y):
# segments = ((x+20,y+10, x+20,y+70), (x+60,y+10, x+60,y+70),
# (x+10,y+30 , x+70,y+30), (x+10, y+50 , x+70, y+50))
# self.area.window.draw_segments(self.gc, segments)
# # self.pangolayout.set_text("Segments")
# # self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout)
# return
# def draw_rectangles(self, x, y):
# self.area.window.draw_rectangle(self.gc, False, x, y, 80, 70)
# self.area.window.draw_rectangle(self.gc, True, x+10, y+10, 20, 20)
# self.area.window.draw_rectangle(self.gc, True, x+50, y+10, 20, 20)
# self.area.window.draw_rectangle(self.gc, True, x+20, y+50, 40, 10)
# self.pangolayout.set_text("Rectangles")
# self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout)
# return
# def draw_arcs(self, x, y):
# self.area.window.draw_arc(self.gc, False, x+10, y, 70, 70,
# 0, 360*64)
# self.area.window.draw_arc(self.gc, True, x+30, y+20, 10, 10,
# 0, 360*64)
# self.area.window.draw_arc(self.gc, True, x+50, y+20, 10, 10,
# 0, 360*64)
# self.area.window.draw_arc(self.gc, True, x+30, y+10, 30, 50,
# 210*64, 120*64)
# self.pangolayout.set_text("Arcs")
# self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout)
# return
# def draw_pixmap(self, x, y):
# pixmap, mask = gtk.gdk.pixmap_create_from_xpm(
# self.area.window, self.style.bg[gtk.STATE_NORMAL], "gtk.xpm")
# self.area.window.draw_drawable(self.gc, pixmap, 0, 0, x+15, y+25,
# -1, -1)
# self.pangolayout.set_text("Pixmap")
# self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout)
# return
# def draw_polygon(self, x, y):
# points = [(x+10,y+60), (x+10,y+20), (x+40,y+70),
# (x+30,y+30), (x+50,y+40)]
# self.area.window.draw_polygon(self.gc, True, points)
# # self.pangolayout.set_text("Polygon")
# # self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout)
# return
# def draw_rgb_image(self, x, y):
# b = 80*3*80*['\0']
# for i in range(80):
# for j in range(80):
# b[3*80*i+3*j] = chr(255-3*i)
# b[3*80*i+3*j+1] = chr(255-3*abs(i-j))
# b[3*80*i+3*j+2] = chr(255-3*j)
# buff = string.join(b, '')
# self.area.window.draw_rgb_image(self.gc, x, y, 80, 80,
# gtk.gdk.RGB_DITHER_NONE, buff, 80*3)
# self.pangolayout.set_text("RGB Image")
# self.area.window.draw_layout(self.gc, x+5, y+80, self.pangolayout)
# return
def main():
gtk.main()
return 0
if __name__ == "__main__":
input_file = raw_input ( "Input the filename of Marmousi model : " )
if input_file == 'stop' : sys.exit()
string.strip(input_file)
horizons = read_segs.read_segs(input_file)
keys = horizons.keys()
keys.sort()
min_X = max_X = None
min_Z = max_Z = None
for key in keys :
for segment in horizons[key] :
for node in segment :
if ( min_X == None ) :
min_X = max_X = node[0]
min_Z = max_Z = node[1]
else :
min_X = min(min_X, node[0])
max_X = max(max_X, node[0])
min_Z = min(min_Z, node[1])
max_Z = max(max_Z, node[1])
min_Z = 0
Drawing_Segs( min_X, max_X, min_Z, max_Z )
main()
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/