-2

I am getting the same error again and again. I want to know why this is happening whenever I run this code. The error is:

ValueError: could not convert string to float: create

I am making a walking man with turtle graphics. This program run for few seconds and then an error pops up and says python exe stopped.

import turtle
from threading import Thread

# Creating window
wn = turtle.Screen()
wn.setup(700,500)
wn.bgcolor("lightblue")
wn.title("johnny Walker")

# Creating circle
jw_head = turtle.Turtle()
jw_head.color("black")
jw_head.pensize(3)
jw_head.circle(30)
jw_head.hideturtle()

# Creating torso.
tor = turtle.Turtle()
tor.color("black")
tor.pensize(3)
tor.right(90)
tor.forward(150)
tor.hideturtle()

# Left hand.
left_hand = turtle.Turtle()
left_hand.color("black")
left_hand.speed(10)
left_hand.pensize(2)
left_hand.penup()
left_hand.setposition(0,-50)
left_hand.pendown()
left_hand.right(90)

# Right hand.
right_hand = turtle.Turtle()
right_hand.color("black")
right_hand.speed(10)
right_hand.pensize(2)
right_hand.penup()
right_hand.setposition(0,-45)
right_hand.pendown()
right_hand.right(90)


def l_hand():
    while True:
        angle = 1
        while angle < 90:
            left_hand.forward(100)
            left_hand.hideturtle()
            left_hand.clear()
            left_hand.penup()
            left_hand.setposition(0,-50)
            left_hand.pendown()
            left_hand.right(1)
            angle = angle + 1
        left_hand.left(90)

def r_hand():
    while True:
        angle1 = 1
        while angle1 < 90:
            right_hand.left(1)
            right_hand.forward(100)
            right_hand.hideturtle()
            right_hand.clear()
            right_hand.penup()
            right_hand.setposition(0,-45)
            right_hand.pendown()
            right_hand.left(1)
            angle1 = angle1 + 1
        right_hand.right(90)


p1 = Thread(target = l_hand)
p2 = Thread(target = r_hand)
p1.start()
p2.start()
p1.join()
p2.join()
cdlane
  • 40,441
  • 5
  • 32
  • 81
vijay Dave
  • 51
  • 1
  • 5
  • Please, Help me to getting out of this trouble. – vijay Dave Mar 13 '18 at 13:22
  • 1
    You should indicate the line on which you get the error... My guess is that the color assignment you are doing is incorrect. – tehhowch Mar 13 '18 at 13:22
  • It gives me error like this:" the pythonexe. has stopped working". – vijay Dave Mar 13 '18 at 14:12
  • 3
    Use code formatting to edit your post and supply the **full** traceback that comes from executing your program. This will indicate exactly where your program went wrong, rather than the unhelpful and not useful `"the pythonexe. has stopped working"` – tehhowch Mar 13 '18 at 14:20

1 Answers1

0

The error I get when running your code is:

RuntimeError: main thread is not in main loop

which I assume is due to executing tkinter (which underlies turtle) code on a secondary thread. You can use threads with tkinter but the graphics need to be shuttled to the main thread.

Let's start over using turtle's built-in ontimer() capability instead of threads. I'm not sure what your figure is supposed to be doing, so I've done a simple animation where he moves his arms from straight down to out to his sides, but at different rates of speed:

from turtle import Turtle, Screen

# Creating window
wn = Screen()
wn.setup(700, 500)
wn.bgcolor('lightblue')
wn.title('johnny Walker')

# Creating circle
jw_head = Turtle(visible=False)
jw_head.speed('fastest')
jw_head.pensize(3)
jw_head.circle(30)

# Creating torso.
tor = Turtle(visible=False)
tor.speed('fastest')
tor.pensize(3)
tor.right(90)
tor.forward(150)

# Left hand.
left_hand = Turtle(visible=False)
left_hand.pensize(2)
left_hand.right(90)
left_hand.forward(50)
left_hand.forward(50)  # intentionally duplicated for undo()

# Right hand.
right_hand = Turtle(visible=False)
right_hand.pensize(2)
right_hand.right(90)
right_hand.forward(50)
right_hand.forward(50)  # intentionally duplicated for undo()

def l_hand():
    left_hand.undo()
    left_hand.left(1)
    left_hand.forward(50)
    wn.update()

    if left_hand.heading() < 359:
        wn.ontimer(l_hand, 100)

def r_hand():
    right_hand.undo()
    right_hand.right(1)
    right_hand.forward(50)
    wn.update()

    if right_hand.heading() > 179:
        wn.ontimer(r_hand, 50)

wn.tracer(False)

l_hand()
r_hand()

wn.mainloop()

If you must use threading, here's an SO example that does Multithreading With Python Turtle

cdlane
  • 40,441
  • 5
  • 32
  • 81