1

I am trying to draw a fixed number of series of vertical stripes with random widths and colours. Unfortunately the kernel remains busy and nothing is drawn by the turtle.

I am using Anaconda - Jupyter I realise I am being quite vague but I genuinely don't have a clue how to tackle this. Really appreciate whoever takes their time to help!

import turtle
from turtle import *
import random

window = turtle.Screen()
window.colormode(255)
window.bgcolor("black")
turtle = turtle.Turtle()
turtle.speed(10)
turtle.penup()
turtle.shape("turtle")
turtle.goto(0,0)
#turtle.mode("standard")

def drawstripes(rgb, length, width):
    turtle.color(rgb)
    turtle.begin_fill()
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(length)
    turtle.end_fill()

n = 20
length = 100
totalwidth = 40
widths = [random.uniform(0, totalwidth) for i in range(0, n)]
widths.sort()

turtle.goto(0,length)
turtle.setheading(270)

while turtle.xcor() <= totalwidth :  
    for width in widths :
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        rgb = (r, g, b)
        drawstripes(rgb, length, width)

As explained, expected result is a series of adjacent vertical stripes with different widths and colours, from x=0 to x=40px

  • Did you check here: [Turtle Graphics Not Responding](https://stackoverflow.com/questions/7217405/turtle-graphics-not-responding)? – Georgy Jun 30 '19 at 20:23
  • Have you tried running it from command-line instead of via Anaconda - Jupyter? – martineau Jun 30 '19 at 21:10
  • I haven't martineau, good shout. @cdlane below came in clutch with the rework. Will try from both console and jupyter. Big thanks. – Filipe Coutinho Jul 01 '19 at 19:28

1 Answers1

1

Below is a rework of your code to work cleanly at the Python console, see if it does any better for you under Anaconda - Jupyter:

from turtle import Screen, Turtle
from random import randint, uniform

n = 20
length = 100
totalwidth = 80

def drawstripe(rgb, length, width):
    turtle.color(rgb)
    turtle.begin_fill()
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(length)
    turtle.left(180)
    turtle.end_fill()

window = Screen()
window.colormode(255)
window.bgcolor("black")

turtle = Turtle()
turtle.shape('turtle')
turtle.speed('fastest')
turtle.penup()

widths = [uniform(0, totalwidth) for i in range(0, n)]
widths.sort()

turtle.sety(length)
turtle.setheading(270)

for width in widths:
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    rgb = (r, g, b)
    drawstripe(rgb, length, width)

    if turtle.xcor() > totalwidth:
        break

turtle.hideturtle()
window.mainloop()

Specific changes: added mainloop() call at end; added final turn in drawstripe() to make turtle progress forward rather than backward; extracted inner loop as it wasn't clear what the outer loop was trying to achieve -- added back functionality of outer as a conditional; doubled totalwidth to make output more obvious.

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Amazing cdlane. Really appreciate it. Really clean and clearly explained changes. I will try and run from Jupyter now and try and work with a more restricted range of colours. Big big thanks. – Filipe Coutinho Jul 01 '19 at 19:32