Home > Net >  How to set turtle tracer false using tkinter?
How to set turtle tracer false using tkinter?

Time:12-05

I have to generate two turtle windows and draw in each one, so I'm using tkinter to create and show the windows. My code currently opens the right screen and draws in it, but the turtle is really slow so I want to set the turtle tracer to false to use the update function, but I can't figure out how to.

This is my turtle_interpreter.py file, which has all the functions I use to draw the L-system:

import turtle
from tkinter import *


class Window(Tk):
    def __init__(self, title, geometry):
        super().__init__()
        self.running = True
        self.geometry(geometry)
        self.title(title)
        self.protocol("WM_DELETE_WINDOW", self.destroy_window)
        self.canvas = Canvas(self)
        self.canvas.pack(side=LEFT, expand=True, fill=BOTH)
        self.turtle = turtle.RawTurtle(turtle.TurtleScreen(self.canvas))


    def update_window(self):
        '''
        sets window to update
        '''
        if self.running:
            self.update()


    def destroy_window(self):
        '''
        sets window to close
        '''
        self.running = False
        self.destroy()


def drawString(turt, dstring, distance, angle):
    '''Interpret the characters in string dstring as a series
    of turtle commands. Distance specifies the distance
    to travel for each forward command. Angle specifies the
    angle (in degrees) for each right or left command. The list   
    of turtle supported turtle commands is:
    F : forward
    - : turn right
      : turn left
    '''
    for char in dstring:
        if char == 'F':
            turt.forward(distance)
        elif char == '-':
            turt.right(angle)
        elif char == ' ':
            turt.left(angle)


def place(turt, xpos, ypos, angle=None):
    '''
    places turtle at given coordinates and angle
    '''
    turt.penup()
    turt.goto(xpos, ypos)
    if angle != None:
        turt.setheading(angle)
    turt.pendown()


def goto(turt, xpos, ypos):
    '''
    moves turtle to given coordinates
    '''
    turt.penup()
    turt.goto(xpos, ypos)
    turt.pendown()


def setColor(turt, color):
    '''
    sets turtle color
    '''
    turt.color(color)

And this is the file where the functions get called. Running it draws the L-system.

import turtle_interpreter as turt_int
import lsystem_scene_three as lsystem

def turtle_scene_two():
    '''
    generates scene two
    '''
    # create window
    win_two = turt_int.Window('Turtle Scene 2', '640x480 650 0')

    # assign turtle
    turt2 = win_two.turtle

    # lsystem setup
    lsystemFile = lsystem.Lsystem('lsystem_scene_two.txt')

    tstr = lsystemFile.buildString(4)

    # draw stuff
    turt_int.setColor(turt2, (0, 0, 0))
    turt_int.place(turt2, 0, -200, 90)
    turt_int.drawString(turt2, tstr, 4, 90)

    # update window (loop)
    while win_two.running:
        win_two.update_window()

turtle_scene_two()

Hope this makes sense. Let me know if it doesn't.

Appreciate your help!

Tried a few things but nothing was promising. Calling turtle generates another screen (which I don't want).

CodePudding user response:

Since you didn't provide all your code, I can't test this, so I'm guessing a good start would be changing this:

self.turtle = turtle.RawTurtle(turtle.TurtleScreen(self.canvas))

to something like:

screen = turtle.TurtleScreen(self.canvas)
screen.tracer(False)
self.turtle = turtle.RawTurtle(screen)
  • Related