Salve, mi sono da poco immerso nel geometrico mondo di pygoocanvas ponendomi, almeno per ora, l'ambizioso obiettivo di disegnare, ridimensionare e spostare rettangoli :-p
Tentando di accumulare una base di conoscenze minime per partire ho cercato di recuperare quanta più documentazione possibile, fermandomi purtroppo alla "sola" guida di riferimento delle API locale(ubuntu#8.04: /usr/share/gtk-doc/html/pygoocanvas/index.html) che non mi è parsa particolarmente esemplificativa per un novellino... Rubando idee agli esempi trovati in rete, sono arrivato a disegnare, con qualche dubbio, il mio primo rettangolo. * Uno dei miei dubbi consultando la guida di riferimento delle API leggo: rect = goocanvas.Rect(x=100, y=100, width=200, height=100, stroke_color="red", fill_color="blue", line_width=5.0) Noto che manca ogni riferimento all'argomento "parent=....". Argomento presente in ogni altro esempio da me trovato e che ho incluso nel codice di esempio che accodo a questa mail. Le prove/riprove fatte, mi hanno portato a pensare che "parent" sia un elemento *necessario* ma non ho trovato riferimenti che mi chiariscano cosa sia e come si debba correttamente usare. * Il secondo scoglio che vorrei superare è quello di capire come ridimensionare un rettangolo in fase di creazione. Il mio intento era in sintesi di: 1) Creare un piccolo(1x1 pixel) rettangolo collegato a un evento "on_mouse_press". 2) Modificare la dimensione del rettangolo creato sfruttando "on_motion_notify". 3) Terminare il disegno del rettangolo con "on_button_release". * L'ultimo scoglio resterà poi quello di spostare il rettangolo creato trascinandolo con il mouse. Qualcuno sarebbe così gentile da suggerirmi qualche riferimento utile o del codice? Grazie in anticipo :-), Simone ps. Accodo il codice a cui sto lavorando: ---python-code-begin--- #!/usr/bin/python import gtk import goocanvas def main_quit(*arg): gtk.main_quit() class ProvaCanvas(object): """ prova canvas """ def __init__(self): # Dimensioni canvas self.canvas_width=400 self.canvas_height=400 self.movement = False def on_button_press(self, widget, event): """ """ self.movement = True print '<button_press> x=%s, y=%s\n' % (event.x, event.y) self.button_press_x=event.x self.button_press_y=event.y def on_button_release(self, widget, event): """ """ self.movement = False print 'button released' width = event.x-self.button_press_x height = event.y-self.button_press_y self.draw_rect(event, width, height) self.draw_text(event, text='prova') def on_motion_notify(self, widget, event): if self.movement: print event.x, event.y def draw_rect(self, event, width=5, height=5, fill='blue'): """ Disegno rettangolo """ x1 = self.button_press_x y1 = self.button_press_y x2 = event.x y2 = event.y if x2 > x1: width = x2-x1 x=x1 else: width = x1-x2 x=x2 if y2 > y1: y=y1 height = y2-y1 else: y=y2 height = y1-y2 print 'rect (\n' print '\t x1: %s \n\t y1: %s \n' % (x1, y1) print '\t x2: %s \n\t y2: %s' % (x2, y2) print '\n\t width: %s \n\t height: %s\n\t)' % (width, height) if (width > 0) and (height > 0): self.rect = goocanvas.Rect(x=x, y=y, width=width, height=height, fill_color=fill, parent=self.root) else: print "Dimensioni nulle, non disegno" def draw_text(self, event, text="no text", font="Arial 10", width=100): """ add text """ x1 = self.button_press_x y1 = self.button_press_y x2 = event.x y2 = event.y if x2 > x1: width = x2-x1 x = x1 else: width = x1-x2 x = x2 if y2 > y1: y = y1 else: y = y2 msg = goocanvas.Text(text=text, font=font, x=x, y=y, width=width) self.root.add_child(msg) def main(self): win = gtk.Window() win.connect('destroy', main_quit) win.set_title('Prova Canvas') vbox = gtk.VBox() win.add(vbox) vbox.show() label = gtk.Label("Prova canvas. disegna rettangoli") vbox.pack_start(label, expand=False) label.show() # Creo canvas self.canvas = goocanvas.Canvas() self.canvas.set_size_request(self.canvas_width,self.canvas_height) self.canvas.set_bounds(0, 0, self.canvas_width,self.canvas_height) vbox.pack_start(self.canvas) self.canvas_width=400 self.canvas_height=400 self.canvas.show() self.canvas.connect('button-press-event', self.on_button_press) self.canvas.connect('button-release-event', self.on_button_release) self.canvas.connect('motion-notify-event', self.on_motion_notify) self.root = self.canvas.get_root_item() hbox = gtk.HBox() vbox.pack_start(hbox, expand=False) hbox.show() b = gtk.Button("Esci") b.connect("clicked", main_quit) hbox.pack_start(b) b.show() #win.show() win.show_all() if __name__ == '__main__': c = ProvaCanvas() c.main() gtk.main() ---python-code-end--- _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python