Changing canvas scroll area on python turtle canvas

Is it possible to change the scroll area on a Python turtle canvas? I want the drawing to move with it, not just the coordinates to move. The look I'm going to do is a side scroller, for example, where the display area of ​​the screen moves to the center of the turtle on the screen.

I tried using turtle.setworldcoordinates(llx, lly, urx, ury)

but from the documentation "This does it screen.reset()

". I've also looked at this SO question , but this includes scrollbars, won't center the turtle easily, and has limited canvas space. I'm looking for something that:

  • Move the display area to the center of the turtle
  • Also moves the drawing
  • Has an infinite scroll area
  • does not display scrollbars
  • can be called quickly with a function My best guess was to be able to have an infinite scrollable canvas, then hide the scrollbars and set them according to the turtle's position.

Is this possible in Python 2.7? I don't mind if it uses tkinter.

EDIT: 6-3-15

I found functions canvas.xview

and canvas.yview

but they don't work as soon as I define screen = turtle.TurtleScreen(canvas)

and TurtleScreen has no functions xview

or yview

. I cannot make this work.

Then I found turtle.ScrolledCanvas()

. This seems to be ideal, except that it has no methods for setting the scrolling manually from within the program. Can I set the scroll manually onturtle.ScrolledCanvas()

+3


source to share


1 answer


The position of the canvas can be changed without reset using the canvas.place () method . It also moves the turtle and the drawings, so the turtle needs to be moved after each move.

The following code moves the canvas with left and right arrows and draws a circle with space, keeping the turtle in the center. No ScrolledCanvas needed, just a very large standard canvas:



import turtle
import Tkinter as tk


def keypress(event):
    global xx, canvas, t, speed
    ev = event.keysym
    if ev == 'Left':
        xx += speed
    else:
        xx -= speed

    canvas.place(x=xx)
    t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)
    return None


def drawCircle(_):
    global t
    t.pendown()
    t.fillcolor(0, 0, 1.0)
    t.fill(True)
    t.circle(100)
    t.fill(False)
    t.fillcolor(0, 1, 0)
    t.penup()

# Set the main window
window = tk.Tk()
window.geometry('500x500')
window.resizable(False, False)

# Create the canvas. Width is larger than window
canvas = turtle.Canvas(window, width=2000, height=500)
xx = -500
canvas.place(x=xx, y=0)

# Bring the turtle
t = turtle.RawTurtle(canvas)
t.shape('turtle')  # nicer look
t.speed(0)
t.penup()
t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)

# key binding
window.bind('<KeyPress-Left>', keypress)
window.bind('<KeyPress-Right>', keypress)
window.bind('<KeyPress-space>', drawCircle)

drawCircle(None)
speed = 3  # scrolling speed
window.mainloop()

      

With real infinite scrolling, you will need to redraw each element in the canvas with the required offset each time, and not move or scroll the canvas. Functions such as create_image () can give the illusion of motion with a static background, but discard pictures.

+1


source







All Articles