Hi, I'm making a gui front end for ucblogo, but I have ran into a problem
using the canvas: 

When I move a turtle and then draw the line, the line isn't drawn
correcty. I can only see the line where the turtle was before and is after
the move.

The problem is hard to put it in word's so I attached an example to put my
words into pictures.

You just run:

python logo_bug.py


If you pres the button "Move bug: actual potition plus x=40+" you'll see
what I mean.

You can write a y ordinate in the entry and press move. For distances
below 38 (the turtle length) relative to the actual position of the turtle
there is no problem, but for distances of 40 and higher the line is not
"drawn".

        I hope that I explain my problem correctly if you have any doubts
        please let me know.


                                        Daniel Kornhauser.

 






#!/usr/bin/env python

from gtk import *
from gnome.ui import *
import gnome.affine
import string
import popen2

class Turtle:
    
    def __init__(self, canvas):
        self.canvas = canvas
        #creating a default graphic turle representation
        self.drawing = self.canvas.root().add('group')
        self.triangle = self.drawing.add('line', 
                                                points=(-5,5,5,5,0,-20,-5,5),
                                                width_pixels=1, 
                                                fill_color='blue')
        self.last_x = 0
        self.last_y = 0
        self.heading = 0
        self.pen_vis = 0

    def move(self,x ="none", y="none"):
        self.drawing.children()[0].affine_relative(
            gnome.affine.translate(x,-(y)))
        if((self.last_x != x or self.last_y != y) and self.pen_vis == 0):
            self.canvas.root().add('line', 
                                   points=(self.last_x,-self.last_y,x, -y),
                                   width_pixels=3, 
                                   fill_color='blue')
            self.last_x = x
            self.last_y = y

class TurtleCanvas:
    def __init__(self):
        self.create_canvas()
        self.create_canvas_controls()
        self.create_logo()

    def create_logo(self):
        self.turtle = Turtle(self.canvas)
        #self.text.insert_defaults("Welcome to Berkeley Logo version 4.6\n")

    def create_canvas(self):
        # Declare the size of the TurtleCanvas
        self.width = 500
        self.height = 500
        # Open window to hold canvas.
        win = GtkWindow()
        win.connect('destroy', mainquit)
        win.set_title('Turtle Canvas ')
        # Create VBox to hold canvas and buttons.
        self.main_vbox = GtkVBox()
        win.add(self.main_vbox)
        self.main_vbox.show()
        # Create canvas.
        self.canvas = GnomeCanvas(aa=TRUE)
        self.canvas.set_usize(self.width, self.height)
        self.canvas.set_scroll_region(-(self.width/2), -(self.height/2),
                                      self.width/2, self.height/2)
        self.main_vbox.pack_start(self.canvas)
        self.canvas.show()
        # Show Evrything.
        win.show()

    def create_canvas_controls(self):
        # Create buttons and entries
        self.control_box = GtkHBox()
        self.main_vbox.pack_start(self.control_box, expand=FALSE)
        self.control_box.show()
        hbox = GtkHBox()
        self.control_box.pack_start(hbox, expand=FALSE)
        hbox.show()

        y_label = GtkLabel("y")
        hbox.pack_start(y_label)
        y_label.show()  
        self.y = GtkEntry()
        hbox.pack_start(self.y)
        self.y.show()

        b = GtkButton("Move")
        b.connect("clicked", self.move)
        hbox.pack_start(b)
        b.show()

        b = GtkButton("Move bug: actual potition plus x=40+")
        b.connect("clicked", self.move_bug)
        hbox.pack_start(b)
        b.show()

        b = GtkButton("QUIT")
        b.connect("clicked", mainquit)
        hbox.pack_start(b)
        b.show()

    def move(self,widget):
        try:
            turtle_y = string.atoi(self.y.get_text())
        except ValueError :
            turtle_y =  self.turtle.last_y
        turtle_x = self.turtle.last_x
        self.turtle.move(x=turtle_x, y=turtle_y)

    def move_bug(self,widget):
        turtle_x = self.turtle.last_x
        turtle_y = self.turtle.last_y+40
        self.turtle.move(x=turtle_x, y=turtle_y)

if __name__ == '__main__':
    gui = TurtleCanvas()
    mainloop()

Reply via email to