Python Turtle, change visible part - python

On the canvas,I draw two dots,right one is at (100,0),left one is at (-1000,0).After initializing the program,the orginal screen location(visible part) is near the right dot,just like pic1 show
pic 1:[1]: https://i.stack.imgur.com/KtPRN.png
And now I wanna move the the screen(visible part) to the left dot using coordinate so that i can see it(pic2).What should I do?
pic 2:https://i.stack.imgur.com/Rtfrv.png
def drawDot(x):
penup()
goto(x, 0)
pendown()
dot('pink')
write(x)
b = -1000 #left dot(-1000,0)
a = 100 #right dot(100,0)
speed(0)
delay(0)
tracer(0, 0)
hideturtle()
screensize(500,500)
color('red')
bgcolor('black')
drawDot(a)
drawDot(b)
done()

I believe the following does what you describe. When the window opens, it's centered on (0, 0) and point a is visible off to the right and point b isn't visible at all. When you click on the window, it scrolls so that the window is centered on point b:
from turtle import Screen, Turtle
WINDOW_WIDTH, WINDOW_HEIGHT = 500, 500
CANVAS_WIDTH, CANVAS_HEIGHT = 3000, 1000
def drawDot(x):
turtle.penup()
turtle.setx(x)
turtle.dot('pink')
turtle.write(x)
def scrollToDot(x, y): # unused arguments
canvas = screen.getcanvas()
# tkinter has a different coordinate system
# we have to describe left edge of scrolled
# window as percentage in its coordinates:
screen_center = CANVAS_WIDTH / 2
dot_center = screen_center + b
left_edge = dot_center - screen.window_width() / 2
canvas.xview_moveto(left_edge / CANVAS_WIDTH) # percentage
a = 100 # right dot(100, 0)
b = -1000 # left dot(-1000, 0)
screen = Screen()
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT) # What we see
screen.screensize(CANVAS_WIDTH, CANVAS_HEIGHT) # What there is
screen.bgcolor('black')
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.color('red')
drawDot(a)
drawDot(b)
screen.onclick(scrollToDot)
screen.mainloop()
To do this, we have to access the tkinter Canvas underpinnings of turtle. However, the Canvas coordinate system is different than turtle's, so we have to make an adjustment as noted in the code comments.

Related

How to convert window coords into turtle coords (Python Turtle)

I am trying to create a program to move the turtle to where the mouse is.
I am doing:
import turtle
t = turtle.Turtle()
canvas = turtle.getcanvas()
while True:
mouseX, mouseY = canvas.winfo_pointerxy()
t.goto(mouseX, mouseY)
but the turtle keeps moving off the screen.
I read from this question that canvas.winfo_pointerxy() returns 'window coordinates' (0, 0 at the top left of the window) and that I need to convert them to 'turtle coordinates' (0, 0 at the center of the window) but I don't know how to do that.
First, you need to find the size of the canvas. for this example, I used a set width and height, but for your purposes, you may find it easier to find the size instead of entering it.
width = 500
height = 300
t = turtle.Turtle()
canvas = turtle.getcanvas()
turtle.screensize(canvwidth=width, canvheight=height)
you can use this code to find the width and height
width = canvas.winfo_width()
height = canvas.winfo_height()
When you measure the mouse's position, however, you'll need to do this calculation to get the right value.
mouseX = canvas.winfo_pointerx() - width/2
mouseY = (canvas.winfo_pointery()*-1) + height/2
t.goto(mouseX, mouseY)
You have to use the dimensions of the window to calculate the center of the window. You just need to subtract the midpoint from the mouse coordinates (since the 0,0 for that is the top-left of the screen) to get it.
You need to add the following:
width = canvas.winfo_width()
height = canvas.winfo_height()
midpoint_x = width / 2
midpoint_y = height / 2
turtleX = mouseX - midpoint_x
turtleY = mouseY - midpoint_y
You end up with something like this:
import turtle
t = turtle.Turtle()
canvas = turtle.getcanvas()
width = canvas.winfo_width()
height = canvas.winfo_height()
midpoint_x = width / 2
midpoint_y = height / 2
while True:
mouseX, mouseY = canvas.winfo_pointerxy()
turtleX = mouseX - midpoint_x
turtleY = mouseY - midpoint_y
t.goto(turtleX, turtleY)

Screen Won't Open When Drawing Checker Board Using Turtle

I am trying to draw a checker board using the Turtle library and am running into an error where the board window does not open. It was working at the beginning of my session about 30 minutes ago but, I changed some stuff and want to know why it changed.
Here is my code:
##This program draws a checkboard using the turtle library
import turtle
#below initiates the turtle pen and screen
penMain = turtle.Turtle()
turtleMain = turtle.Screen()
def turtleBoard():
for x in range(4):
penMain.forward(30)
penMain.left(90)
penMain.forward(30)
turtleMain.setup(600, 600)
penMain.speed(50)
for a in range(8):
penMain.up()
penMain.setpos(0, 30 * a)
penMain.down()
for x in range(8):
if (a + x)% 2 == 0:
squareColor = 'black'
else:
squareColor = 'white'
penMain.fillcolor(squareColor)
penMain.begin_fill()
turtleBoard()
penMain.end_fill()
I believe this code works besides my one error! Thank you all for your help in advance!
I can't say what changes you made to get your current code, but this code seems to be working:
##This program draws a checkboard using the turtle library
import turtle
#below initiates the turtle pen and screen
penMain = turtle.Turtle()
turtleMain = turtle.Screen()
def turtleBoard():
penMain.forward(30)
turtleMain.setup(600, 600)
penMain.speed(50)
for a in range(8):
for x in range(8):
penMain.up()
penMain.setpos(30 * x, 30 * a)
penMain.down()
penMain.begin_fill()
for xx in range(4):
penMain.forward(30)
penMain.left(90)
if a%2 == x%2:
squareColor = 'black'
else:
squareColor = 'white'
penMain.fillcolor(squareColor)
penMain.end_fill()
turtleBoard()
turtle.done()
Now that we've seen that your code can be made to work, let's consider stamping instead of drawing to make it work more simply and more quickly:
from turtle import Screen, Turtle
SQUARES_PER_EDGE = 8
SQUARE_SIZE = 30 # in pixels
OFFSET = SQUARE_SIZE * (SQUARES_PER_EDGE / 2) - SQUARE_SIZE/2 # center the board
CURSOR_SIZE = 20
def turtleBoard():
turtle.shape('square')
turtle.shapesize(SQUARE_SIZE / CURSOR_SIZE)
turtle.penup()
for y in range(SQUARES_PER_EDGE):
for x in range(SQUARES_PER_EDGE):
turtle.goto(x * SQUARE_SIZE - OFFSET, y * SQUARE_SIZE - OFFSET)
turtle.fillcolor('black' if y % 2 == x % 2 else 'white')
turtle.stamp()
screen = Screen()
screen.setup(600, 600)
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
turtleBoard()
screen.exitonclick()
I lined up the indent of the bottom 4 lines with the last 'else' statement and it worked. Thank you guys!

Turtle Graphics Hexagon Centering

I need some help. I want to center the hexagon into the larger hexagon but I don't now how to do it. Below I have the source code and an image link to the output.
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 20
move_left = 60
polygon.pensize(2)
polygon.pencolor((245, 176, 66))
for turtle_move in range(num_sides):
polygon.forward(side_length)
polygon.left(move_left)
polygon.penup()
polygon.left(2)
polygon.pendown()
side_length2 = 40
move_left2 = 60
I want to center the hexagon inside the larger hexagons, but I don't know what to do.
for turtle_move in range(num_sides):
polygon.forward(side_length2)
polygon.left(move_left2)
Here is the output:
There are any number of ways to do this if you read about the geometry of hexagons, eg. on Wikipedia:
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
ANGLE_LEFT = 60
screen = Screen()
turtle = Turtle()
for _ in range(NUM_SIDES):
turtle.forward(SIDE_LENGTH)
turtle.left(ANGLE_LEFT)
turtle.penup()
turtle.backward(SIDE_LENGTH / 2)
turtle.sety(-SIDE_LENGTH * 3**0.5/2)
turtle.pendown()
for _ in range(NUM_SIDES):
turtle.forward(SIDE_LENGTH*2)
turtle.left(ANGLE_LEFT)
turtle.hideturtle()
screen.exitonclick()
One alternate approach is to use the turtle circle() method to draw the hexagons, then it becomes a matter of centering two circles:
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
circumradius = SIDE_LENGTH
screen = Screen()
turtle = Turtle()
for _ in range(2):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
turtle.circle(circumradius, steps=NUM_SIDES)
circumradius *= 2
turtle.hideturtle()
screen.exitonclick()
I'm assuming by your use of pencolor((245, 176, 66)) you're using a site like Repl.it or some other non-standard Python turtle implementation, so you may need to adjust the examples above slightly to suit your environment.

Why does turtle open an even smaller screen when the canvas is small?

I'm trying to draw on a small 200x200 screen using turtle, however the drawing doesn't pop up as full size, it opens a smaller window and I have to scroll up/down, left/right (just a bit) to see the whole drawing. I don't have this problem with larger windows. How do I prevent this?
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
edit: screenshot, I want the actual size window instead of the scrolls
You could resize the window to 420×420.
If you don't want to resize your window, I suggest modifying the values for the keys "canvwidth" and "canvheight" keys in the turtle._CFG dictionary:
import turtle
import random
height, width = 200, 200
screen = turtle.Screen()
screen.setup(width, height)
screen.setworldcoordinates(0, 0, width, height)
turtle._CFG.update({"canvwidth": width-20, "canvheight": height-20}) # Removing the scroll bars
t = turtle.Turtle()
t.speed(1)
for _ in range(5):
t.penup()
t.goto(random.randint(20, width-20), random.randint(0, height-40))
t.pendown()
t.circle(20)
screen.exitonclick()
Using small windows in turtle is a can of worms. If #TheOneMusic's simple solution (+1) is good enough for your purposes, go for it! On my system, your setworldcoordinates() call gets rid of the scroll bars, so I don't even see the issue. So, another approximate solution might be to upgrade to current Python and tkinter.
However, neither is an exact solution. If we add code to draw a 200 x 200 box around our drawing area:
t.penup()
t.color('red')
t.goto(0, 0) # because of setworldcoordinates()
t.pendown()
for _ in range(4):
t.forward(200)
t.left(90)
We get the box skewed:
To solve this problem more precisely, involves uglier code:
from turtle import Screen, Turtle
from random import randint
TRUE_WIDTH, TRUE_HEIGHT = 200, 200
CURSOR_SIZE = 20 # for drawing frame around edge
RADIUS = 20
CHROME = 14 # magic number possibly derivable from tkinter
width, height = TRUE_WIDTH + CHROME, TRUE_HEIGHT + CHROME # needs to be slightly larger than 200 target
offset_x = CHROME / -2 + 2
offset_y = CHROME / 2 - 2
screen = Screen()
screen.setup(width, height)
screen.screensize(width/2, height/2) # backing store needs to be smaller than window
screen.setworldcoordinates(0, 0, TRUE_WIDTH, TRUE_HEIGHT)
# Draw red frame around edge to "prove" drawing area
frame = Turtle(shape='square', visible=False)
frame.shapesize(TRUE_HEIGHT / CURSOR_SIZE, TRUE_WIDTH / CURSOR_SIZE) # 200 x 200 frame
frame.color('red', 'white')
frame.penup()
frame.goto(TRUE_WIDTH/2 + offset_x, TRUE_HEIGHT/2 + offset_y)
frame.stamp()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
for _ in range(5):
turtle.penup()
turtle.goto(randint(RADIUS, TRUE_WIDTH - RADIUS) + offset_x, randint(0, TRUE_HEIGHT - RADIUS*2) + offset_y)
turtle.pendown()
turtle.circle(RADIUS)
screen.exitonclick()
But this sort of detail work could easily be undone by a future release of turtle and/or tkinter. If you can live with turtle's default window, life gets easier.

Tie circle radius to window size in Python Turtle

In Python turtle, when drawing objects on the screen, if there was a way to have a circle's radius connected to the window width or height, so that it can be resized by altering the window size?
Yes, it is possible.
You will need to create an event that the turtle will listen to: In the following example, if you click on the turtle, a circle of half the width of the canvas will be drawn.
If you resize the canvas, and click on the turtle again, a new circle of half the
new width will be redrawn.
import turtle
def start(dummy_a, dummy_b):
t.reset()
y, x = screen.window_height(), screen.window_width()
t.home()
t.circle(x/4)
if __name__ == '__main__':
screen = turtle.Screen()
t = turtle.Turtle()
t.onclick(start, add=True)
screen.listen()
turtle.done()
Here's my alternative click to adjust drawing to resized window solution:
from turtle import Turtle, Screen
def onResize(x=0, y=0):
screen.onclick(None) # disable events inside event handler
screen.setworldcoordinates(-1, -1, 1, 1)
screen.onclick(onResize)
screen = Screen()
onResize() # establish initial coordinate system
turtle = Turtle(visible=False)
turtle.penup()
turtle.sety(-0.5)
turtle.pendown()
turtle.circle(0.5, steps=30)
screen.mainloop()
Note that we're not redrawing anything, we're just readjusting our virtual coordinates (unit square in this example) and letting turtle redraw things. If we're willing to peek under the hood, we can take this one step further:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas
class MyTurtleScreen(TurtleScreen):
def __init__(self, cv):
super().__init__(cv)
cv.bind('<Configure>', self.onResize)
def onResize(self, event=None):
self.setworldcoordinates(-1, -1, 1, 1)
root = tk.Tk()
canvas = ScrolledCanvas(root)
canvas.pack(fill=tk.BOTH, expand=tk.YES)
screen = MyTurtleScreen(canvas)
screen.onResize() # establish initial coordinate system
turtle = RawTurtle(screen, visible=False)
turtle.penup()
turtle.sety(-0.5)
turtle.pendown()
turtle.circle(0.5, steps=30)
screen.mainloop()
This is a generic embedding turtle in tkinter example except that I've customized TurtleScreen to accept a Configure event. Now, when you resize the window, the coordinate system trick from before kicks in automatically so you don't need to click on the window -- it just happens.

Categories

Resources