How Do I Get The Turtle Canvas Screen To Move With A Turtle Module?
Solution 1:
Below is an example of manipulating the scrolling at the tkinter level to obtain a desired behavior. In this case, I'm keeping the turtle stationary at the center of the screen but using the arrow keys to move the landscape beneath it.
The code between ### Generate a landscape ...
and ### Finished generating a ...
is from a SO question I answered earlier and is there to make an interesting fractal landscape to explore:
from turtle import Turtle, Screen
from random import random
MAGNIFICATION = 10defmove_left():
canvas.xview_scroll(-1, "units")
turtle.setx(turtle.xcor() - MAGNIFICATION)
defmove_right():
canvas.xview_scroll(1, "units")
turtle.setx(turtle.xcor() + MAGNIFICATION)
defmove_up():
canvas.yview_scroll(-1, "units")
turtle.sety(turtle.ycor() + MAGNIFICATION)
defmove_down():
canvas.yview_scroll(1, "units")
turtle.sety(turtle.ycor() - MAGNIFICATION)
screen = Screen()
width, height = screen.screensize()
screen.screensize(width * MAGNIFICATION, height * MAGNIFICATION)
canvas = screen.getcanvas()
canvas.config(xscrollincrement=str(MAGNIFICATION))
canvas.config(yscrollincrement=str(MAGNIFICATION))
# turtle initialization
turtle = Turtle("turtle", visible=False)
turtle.width(MAGNIFICATION)
turtle.resizemode('auto')
### Generate a landscape to explore
screen.tracer(False)
RULES = {'x':'x+yf+', 'y':'-fx-y', 'f':'f', '+':'+', '-':'-'}
sub_string = string = "fx"
LEVEL = 13for _ inrange(LEVEL):
turtle.pencolor(random(), random(), random())
for character in sub_string:
if character == '+':
turtle.right(90)
elif character == '-':
turtle.left(90)
elif character == 'f':
turtle.forward(5 * MAGNIFICATION)
screen.update()
full_string = "".join(RULES[character] for character in string)
sub_string = full_string[len(string):]
string = full_string
screen.tracer(True)
### Finished generating a landscape to explore
turtle.penup()
turtle.home()
turtle.setheading(90)
turtle.color('dark green', 'light green')
turtle.showturtle()
screen.onkey(move_left, "Left")
screen.onkey(move_right, "Right")
screen.onkey(move_up, "Up")
screen.onkey(move_down, "Down")
screen.listen()
screen.mainloop()
The scroll bars reflect the movement about the entire space. Unfortunately, the scroll bars are still active and will throw things askew (use the arrow keys instead), and it needs work near the landscape edges, but this is simply an example to show that pretty much anything is possible if you take the time to explore the tkinter/Tk underpinnings.
Post a Comment for "How Do I Get The Turtle Canvas Screen To Move With A Turtle Module?"