Is it possible to change the position of the turtle console on screen?
My main objective is to write code that can move the window, that's all.
I'm using Python 3.4.0 under Windows 10.
If any extra information is needed please ask.
Why do folks always jump into tkinter before reading the turtle documentation?
Yes, you can set the screen position of the turtle graphics window using the same setup() method you use to size it:
from turtle import Turtle, Screen
def animate():
global offset
screen.setup(width=0.333, height=0.333, startx=offset, starty=offset)
turtle.dot(offset)
offset += 10
if offset < 300:
screen.ontimer(animate, 100)
screen = Screen()
turtle = Turtle()
offset = 30
animate()
screen.exitonclick()
startx, if positive, is the starting position in pixels from the left edge of the screen, or from the right edge if negative. Similarly, starty, if positive, is the starting position from the top edge of the screen, or from the bottom edge if negative. By default, the window is centered on the screen.
Your title asks about the position of the Turtle Graphics window on the screen but the body of your question asks about the Turtle Console. These might be considered two different windows.
My main objective is to write code that can move the window
I can't tell if you just want to set the initial position of the window or actually move the window around the screen so I rewrote my example to demonstrate the later.
Yes. You need to get the root window that contains the Tkinter Canvas that the turtle is using as its TurtleScreen. Once you have that window you can change its geometry.
Here's a simple demo.
import turtle
turtle.setup(width=0.5, height=0.5)
screen = turtle.Screen()
width, height = screen.window_width(), screen.window_height()
canvas = screen.getcanvas()
left, top = 30, 100
geom = '{}x{}+{}+{}'.format(width, height, left, top)
canvas.master.geometry(geom)
t = turtle.Turtle()
turtle.exitonclick()
Related
I have a function that draws a circle based on the screensize of the window. Currently, if I make the window smaller, the entire circle isn't visible. Is there any way to find out if the window has been resized so I can redraw the circle to fit in the window?
Here's my code to draw the circle:
def draw_circle():
# Get circle radius
screensize = s.screensize()
if screensize[0] > screensize[1]:
smaller = 1
else:
smaller = 0
radius = screensize[smaller]
# Draw circle
t.pensize(15)
t.pu()
t.goto(0, -radius)
t.pd()
t.circle(radius)
t.pu()
I have tried to add an if statement that checks if the screensize changes in my main loop, but that didn't work either.
P.S. I tried searching Google and StackOverflow and found no answers.
There's no need to poll in a loop. Once you get the underlying Tkinter canvas with turtle.getcanvas(), you can look up the non-turtle Tkinter approach and use that. For example, adapting Tkinter track window resize specifically? to turtle gives the following minimal example:
import turtle
def resize(event):
print(event.width, event.height)
turtle.getcanvas().bind("<Configure>", resize)
turtle.exitonclick()
Note that this doesn't change the screen size, so you can draw your circle relative to the window instead:
import turtle
def draw():
r = min(turtle.window_width(), turtle.window_height()) / 2 - 30
turtle.clear()
turtle.penup()
turtle.begin_fill()
turtle.goto(0, -r)
turtle.pendown()
turtle.circle(r)
turtle.end_fill()
turtle.update()
def resize(event):
draw()
turtle.tracer(0)
turtle.hideturtle()
turtle.getcanvas().bind("<Configure>", resize)
draw()
turtle.exitonclick()
Drawing relative sizes for everything tends to be pretty tedious, so I recommend avoiding this if at all possible for your app.
In this simple Python Turtle Graphics example, the furthest position available to click for the right hand-side is 245 and for the bottom of the window -245. For the left and top, -250 and 250 are possible So there are some pixels missing from the right and bottom of the window. Adding 5 to the width and height increases the size in the left and up directions, so doesn't fix the problem.
Can someone explain what is going on here please? Is there a way to specify exact dimensions for the turtle window?
import turtle
def click(x, y):
print(x, y)
screen = turtle.Screen()
screen.setup(500, 500)
screen.onclick(click)
turtle.done()
I am trying to set the starting window for my first turtle script, but after some searching it seems that there is no clean way to do it.
after some searching it seems that there is no clean way to do it
What do you mean by clean? You can set the initial size and starting position of the turtle window using the setup() method:
>>> import turtle
>>> help(turtle.setup)
Help on function setup in module turtle:
setup(width=0.5, height=0.75, startx=None, starty=None)
Set the size and position of the main window.
Arguments:
width: as integer a size in pixels, as float a fraction of the
Default is 50% of
height: as integer the height in pixels, as float a fraction of the
Default is 75% of
startx: if positive, starting position in pixels from the left
edge of the screen, if negative from the right edge
Default, startx=None is to center window horizontally.
starty: if positive, starting position in pixels from the top
edge of the screen, if negative from the bottom edge
Default, starty=None is to center window vertically.
For example:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(300, 300, startx=100, starty=200)
turtle = Turtle()
turtle.dot(100)
screen.mainloop()
With Python 3.6 and the Turtle module, I would like to know how to print the coordinates of the turtle on screen.
My questions are: How do I print text on the screen, and how do I make that text the player's coordinates?
Here is my code.
from turtle import Turtle, Screen
play = Screen()
play.bgcolor("black")
play.screensize(250, 250)
play.title("Turtle Keys")
def position():
coord = follow.coor()
coord.color("white")
coord.setposition(130, 100)
run = Turtle("triangle")
run.speed("fastest")
run.color("white")
run.penup()
run.setposition(250, 250)
print(position)
Thank you.
Edit I tried write, but it throws a name not defined error.
Here is how you can write the turtle position on screen: the write method from the turtle module, must be called on a Turtle object.
import turtle
playground = turtle.Screen() # use nouns for objects, play is a verb
playground.bgcolor("black")
playground.screensize(250, 250)
playground.title("Turtle Keys")
tom = turtle.Turtle() # use nouns for objects, run is a verb
tom.color("white")
tom.setposition(130, 100)
tom.speed("fastest")
tom.color("white")
p = tom.pos() # here you get the coordinates of the turtle
tom.write(str(p), True) # and you print a string representation on screen
tom.penup()
print(p) # this prints to the console
playground.exitonclick()
The way I approach this type of problem is to dedicate a turtle to each fixed label on the screen. That way you can permanentely locate a turtle at that position and simply do undo() to clear the old text followed by write() to write the new.
Here's a dynamic example where you can drag the turtle around the screen with your mouse and the current position will be written to the center of the screen:
from turtle import Turtle, Screen
FONT = ('Arial', 24, 'normal')
playground = Screen()
playground.setup(500, 500)
playground.bgcolor("black")
runner = Turtle("triangle")
runner.color("white")
runner.speed("fastest")
runner.penup()
runner.setposition(200, 200)
marker = Turtle(visible=False) # our virtual magic marker
marker.penup()
marker.color("yellow")
marker.write(runner.position(), align='center', font=FONT) # so we can undo it
def drag_handler(x, y):
runner.ondrag(None) # disable handler while in handler
marker.undo() # erase previous position
marker.write((x, y), align='center', font=FONT)
runner.setheading(runner.towards(x, y)) # head towards new location
runner.setposition(x, y)
runner.ondrag(drag_handler)
runner.ondrag(drag_handler)
playground.mainloop()
Use the functions xcor() and ycor() to get coordinates. Use the function write() to write on the screen.
See documentation: https://docs.python.org/3/library/turtle.html
Maybe this will help:
def write_pos(self, position):
self.pu()
self.bk(len(str(position))-len(str(position))/2)
self.pd()
self.write(str(position))
self.pu()
self.fd(len(str(position))-len(str(position))/2)
self.pd()
write_pos(run, run.position())
Ok. Here's the explanation.
So I defined a function writing the position of something (in this case, the turtle).
When I do:
self.pu()
self.bk(len(str(position))-len(str(position))/2)
I:
Lift the pen up and
Move backward some steps, so I can center the text.
I then do:
self.pd()
self.write(str(position))
Which:
Puts the pen down and
Writes (or outputs) the position
After that, when I do:
self.pu()
self.fd(len(str(position))-len(str(position))/2)
self.pd()
I:
Pick the pen up
Move it back to the original position and
Put the pen down again
EDIT:
I just realized that you can do it a much more simple way of using write like this:
write(run.position(), False, center)
It writes the turtles position, it doesn't move, and it still writes the text but with the canter as the place where it aligns.
EDIT #2:
I already figured out the problem.
Your using write directly and not actually calling it from the object. You should call write like this:
run.write(run.position(), align="center")
I would like to create a program where one Turtle object always stays above all of the other Turtle objects. I don't know if this is possible, but any help would be apprecated.
This is my code:
from turtle import *
while True:
tri = Turtle()
turtle = Turtle()
tri.pu()
tri.pencolor("white")
tri.color("black")
tri.shape("turtle")
tri.bk(400)
turtle = Turtle()
turtle.pu()
turtle.pencolor("white")
turtle.shape("square")
turtle.color("white")
turtle.pu()
turtle.speed(0)
tri.speed(0)
turtle.shapesize(100,100,00)
setheading(towards(turtle))
while tri.distance(turtle) > 10:
turtle.ondrag(turtle.goto)
tri.setheading(tri.towards(turtle))
tri.fd(5)
clearscreen()
Why not just do all the drawing for the "bottom" turtle first? Then do the drawing for the "top" turtle? This should make the top turtle always visible.
My Observed Rules of Turtle Layering:
Multiple Turtles moving to same location: last to arrive is on top.
Same thing drawn by multiple turtles: there are no rules!
To illustrate my second point, consider this code:
from turtle import Turtle, Screen
a = Turtle(shape="square")
a.color("red")
a.width(6)
b = Turtle(shape="circle")
b.color("green")
b.width(3)
b.goto(-300, 0)
b.dot()
a.goto(-300, 0)
a.dot()
a.goto(300, 0)
b.goto(300, 0)
screen = Screen()
screen.exitonclick()
Run it and observe the result. On my system, the final goto() draws a long green line over the red one but the green line disappears as soon as it has finished drawing. Comment out the two calls to dot() and observe again. Now the green line remains over the red one. Now change the calls from dot() to stamp() or circle(5) instead. Observe and formulate your own rule...
Now back to your example, which is badly flawed (you're actually manipulating three turtles, not two!) Here's my simplification:
from turtle import Turtle, Screen
tri = Turtle(shape="turtle")
tri.color("black")
tri.pu()
turtle = Turtle(shape="square")
turtle.shapesize(4)
turtle.color("pink")
turtle.pu()
def drag_handler(x, y):
turtle.ondrag(None)
turtle.goto(x, y)
turtle.ondrag(drag_handler)
turtle.ondrag(drag_handler)
tri.bk(400)
while tri.distance(turtle) > 10:
tri.setheading(tri.towards(turtle))
tri.fd(5)
screen = Screen()
screen.mainloop()
You can tease tri by dragging the pink square until tri catches up with it. Ultimately, tri will land on top as long as the square isn't moving when tri catches it. If you drag the square over tri, then it will temporarily cover him as it is the "last to arrive".