简体   繁体   中英

python turtle goto is not working

I have written a turtle programma that uses data to make a line on a graph chart. Everything works fine except i call the make_line() in the chartlines. The goto is excecuted but doesn't draw a line. Even if i put a turtle.down() before i go in to the While loop it doesn't draw a line.

import turtle


def Change_tekenpunt(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()


def xAxisTurtle(days):

    xAxis = turtle
    xAxis.ht()
    Change_tekenpunt(-300, -220)
    x = -275
    for i in range(0,days+1):
        xAxis.write(i)
        Change_tekenpunt(x, -220)
        x+=25

def yAxisTurtle():
    yAxis = turtle
    yAxis.ht()
    Change_tekenpunt(-310, -200)
    y = -160
    for i in range(0, 1601, 200):
        yAxis.write(i)
        Change_tekenpunt(-330, y)
        y+=50

def point():
    turtle.begin_fill() # Begin to fill color in a shape
    turtle.color("black")
    turtle.circle(3) # Draw a circle
    turtle.end_fill() # Fill the shape

def make_line(x, y,gegevens):
    Change_tekenpunt(x,y)
    i = 0 
    while i < len(gegevens):
        y = gegevens[i][0]
        y //= 4
        Change_tekenpunt(x,y)
        x+=25
        i+=1


def chartlines(x,y,days, gegevens):

      char = turtle
      Change_tekenpunt(x,y) 
      char.tracer(0,0)
      char.speed(0)
      char.ht()
      point()
      for i in range(days):      
          char.forward(25)
          char.left(270)
          point()
          char.left(90)

      char.left(180)
      char.forward(25*days)
      char.left(270)

      for i in range(8):
          char.forward(50)
          char.left(90)
          point()
          char.left(270)

      char.left(180)
      char.forward(400)

      xAxisTurtle(days)
      yAxisTurtle()
      make_line(x, y,gegevens)

The problem is in Change_tekenpunt function. You need to keep the turtle.down() while drawing the graph. So, you'll need to make some changes to the Change_tekenpunt function as follows:

def Change_tekenpunt(x,y,up=True):
    if up:
        turtle.up()
    turtle.goto(x,y)
    turtle.down()

You use this function to draw the chart lines and also the graph. I've added an extra up argument (which defaults to True ) because you need to set this to False whenever you're drawing a graph. It has to be down while it's moving. Therefore, in the make_line function, in the while-loop, add the False argument to the Change_tekenpunt function call.

def make_line(x, y,gegevens):
    Change_tekenpunt(x,y)
    i = 0 
    while i < len(gegevens):
        y = gegevens[i][0]
        y //= 4
        Change_tekenpunt(x, y, False) #add  the False argument here
        x+=25
        i+=1

Finally, in the chartlines function, set the .tracer to (1, 0) .

char.tracer(0,0)

char.tracer(1,0)

Or you can instead add turtle.update() at the end of the Change_tekenpunt function. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM