Home > Back-end >  creating boxes using turtle
creating boxes using turtle

Time:03-14

can someone please help me making few boxes, each on different axis co-ordinates using turtle,
P.S. trying to use class and objects in below code:

import turtle
from turtle import *

# window:
window = turtle.Screen()
window.bgcolor("white")
window.title("Process flow")


class a:
    penup()
    shape("square")
    speed(0)

    def __init__(self, reshape, color, location):
        self.reshape = reshape
        self.color = color
        self.location = location


start_node1 = a(reshape=shapesize(stretch_wid=1, stretch_len=3), color=color("light blue"), location=goto(0, 300))
start_node2 = a(reshape=shapesize(stretch_wid=1, stretch_len=3), color=color("yellow"), location=goto(0, 270))


print(start_node1)
print(start_node2)


done()

CodePudding user response:

You seem to have strung together random bits of code with the hope that it would work. E.g. argument calls like:

..., location=goto(0, 300)

pass None onto your initializer as that's what goto() returns. And importing turtle two different ways:

import turtle
from turtle import *

is a sign you're in conceptual trouble. Below is my rework of your code to display boxes of different colors and sizes at various locations that tries to retain as much of the "flavor" of your original code as possible:

from turtle import Screen, Turtle

class Box(Turtle):
    def __init__(self, reshape, color, location):
        super().__init__(shape='square', visible=False)

        self.shapesize(**reshape)
        self.color(color)
        self.speed('fastest')
        self.penup()
        self.goto(location)

        self.showturtle()

screen = Screen()
screen.title("Process flow")

start_node1 = Box(reshape={'stretch_wid':1, 'stretch_len':3}, color='light blue', location=(100, 300))
start_node2 = Box(reshape={'stretch_wid':2, 'stretch_len':4}, color='yellow', location=(-100, 270))

screen.exitonclick()
  • Related