I have been trying to design the below image using Python turtle graphics.
So far, this is what I have come up with.
Here is my code snippet.
from turtle import Turtle
t=Turtle()
t.hideturtle()
t.speed(0)
def text():
t.color("blue")
t.write("AOL",align="center",font=("Adobe Gothic Std B",100,"bold"))
def circle_with_color_fill():
t.up()
t.setpos(200,50)
t.down()
t.color("blue")
t.begin_fill()
t.circle(40)
t.end_fill()
def circle():
t.up()
t.setpos(200,40)
t.down()
t.color("blue")
t.circle(50)
text()
circle_with_color_fill()
circle()
t.screen.mainloop()
Can anyone suggest how I can get those triangles?
Since you have the text worked out, here's a rough approximation of the logo using stamping:
from turtle import Turtle, Screen
STAMP_UNIT = 20
screen = Screen()
aol = Turtle(shape='triangle', visible=False)
aol.color('blue')
aol.turtlesize(140 / STAMP_UNIT)
aol.stamp()
aol.shape('circle')
aol.turtlesize(80 / STAMP_UNIT, outline=12)
aol.color(screen.bgcolor(), 'blue')
aol.stamp()
screen.exitonclick()
OUTPUT
Related
When I attempt to put text over a turtle in the python turtle module, it flashes. Any solutions?
import turtle
s = turtle.Screen()
s.setup(width=500,height=600)
c = turtle.Turtle()
c.shapesize(stretch_len=5,stretch_wid=5)
c.goto(0,0)
c.shape("square")
pen = turtle.Turtle()
pen.hideturtle()
pen.goto(0,0)
pen.color("red")
while True:
pen.write("hello!")
s.update()
Although I don't see the flashing on my screen, my guess is your problem is related to this bad idiom:
while True:
# ...
s.update()
We need neither the while True: (which has no place in an event-driven environment like turtle) nor the call to update() (which is not needed given no previous call to tracer()). Let's rewrite this as turtle code:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=500, height=600)
turtle = Turtle()
turtle.hideturtle()
turtle.shapesize(5)
turtle.shape('square')
turtle.stamp() # stamp a square so we can reuse turtle
pen = Turtle()
pen.hideturtle()
pen.color("red")
pen.write("Hello!", align='center', font=('Arial', 16, 'normal'))
screen.exitonclick()
Does that solve your problem?
I am making an analog clock with python, using turtle module.
I could configue a window size and a circle, But i can't finish outliner of a circle.
I didn't give any integer to get the complete ouliner. But it doesn't surround a circle entire.
Please, tell me if there is a something missing.
window = turtle.Screen()
window.setup(1000, 800)
window.bgcolor("black")
window.title("Analog Clock")
window.tracer(0)
t = turtle.Turtle()
t.color("#FFD700")
t.penup()
t.speed(0)
t.pensize(25)
t.hideturtle()
t.goto(0, -290)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(300, 360)
t.end_fill()
thank you
The problem is your use of tracer() without any call to update(). You can either remove the window.tracer(0) line, or add a window.update() line after your t.end_fill() line.
I usually recommend avoiding tracer() and update() until you have your code basically working, just to avoid bugs like this.
from turtle import Screen, Turtle
screen = Screen()
screen.setup(1000, 800)
screen.bgcolor('black')
screen.title("Analog Clock")
screen.tracer(0)
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest') # no-op if `tracer(0)`
turtle.color('#FFD700')
turtle.pensize(25)
turtle.penup()
turtle.sety(-290)
turtle.pendown()
turtle.fillcolor('white')
turtle.begin_fill()
turtle.circle(300)
turtle.end_fill()
screen.update()
screen.mainloop()
Here is the solution to your problem with your posted code working properly :)
window = turtle.Screen()
window.setup(1000, 800)
window.bgcolor("black")
window.title("Analog Clock")
t = turtle.Turtle()
t.color("#FFD700")
t.penup()
t.speed(0)
t.pensize(25)
t.hideturtle()
t.goto(0, -290)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(300, 360)
t.end_fill()
window.update()
I am trying to make a program that will repeatedly draw two lines. my code is
import turtle
screen = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
screen.tracer(0,0)
while True:
screen.clear()
t.penup()
t.goto(1,12)
t.pendown()
t.goto(4,67)
t.penup()
t.goto(50, 3)
t.pendown()
t.goto(4, 73)
screen.update()
I expect this to show two lines in turtle which do not flicker. however it is drawing one line and that line is flickering. the lines do need to be redrawn every frame so i can do some other stuff with the lines. Why is this happening?
Short answer: don't do screen.clear(), instead do t.clear().
When you clear the screen, you reset a number of its properties to the default values, including the tracer() setting. You simply want to clear whatever the turtle drew in the last iteration so clear the turtle instead.
In the long term, you don't want while True: in an event-driven environment like turtle. I would write this code more like:
from turtle import Screen, Turtle
def one_step():
turtle.clear()
turtle.penup()
turtle.goto(1, 12)
turtle.pendown()
turtle.goto(4, 67)
turtle.penup()
turtle.goto(50, 3)
turtle.pendown()
turtle.goto(4, 73)
screen.update()
screen.ontimer(one_step, 50)
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
one_step()
screen.mainloop()
I am trying to move the turtle to my cursor to draw something so that every time I click, the turtle will go there and draw something.
I have already tried onscreenclick(), onclick, and many combinations of both, I feel like I am doing something wrong, but I don't know what.
from turtle import*
import random
turtle = Turtle()
turtle.speed(0)
col = ["red","green","blue","orange","purple","pink","yellow"]
a = random.randint(0,4)
siz = random.randint(100,300)
def draw():
for i in range(75):
turtle.color(col[a])
turtle.forward(siz)
turtle.left(175)
TurtleScreen.onclick(turtle.goto)
Any help would be great, thank you for your time ( If you help me! ;)
It's not so much what method you're invoking, but on what object you're invoking it:
TurtleScreen.onclick(turtle.goto)
TurtleScreen is a class, you need to call it on a screen instance. And since you want to call draw in addition to turtle.goto you need to define your own function that calls both:
screen = Screen()
screen.onclick(my_event_handler)
Here's a rework of your code with the above fixes and other tweaks:
from turtle import Screen, Turtle, mainloop
from random import choice, randint
COLORS = ["red", "green", "blue", "orange", "purple", "pink", "yellow"]
def draw():
size = randint(100, 300)
# make turtle position center of drawing
turtle.setheading(0)
turtle.setx(turtle.xcor() - size/2)
turtle.pendown()
for _ in range(75):
turtle.color(choice(COLORS))
turtle.forward(size)
turtle.left(175)
turtle.penup()
def event_handler(x, y):
screen.onclick(None) # disable event handler inside event handler
turtle.goto(x, y)
draw()
screen.onclick(event_handler)
turtle = Turtle()
turtle.speed('fastest')
turtle.penup()
screen = Screen()
screen.onclick(event_handler)
mainloop()
I need to draw 4 differently colored triangles on top of each other. I've figured out how to draw 4 next to each other, but I don't manage to get them on top of each other. This is my code:
import turtle
import math
from random import randint
otto = turtle.Turtle()
def repeat_triangle(t, l):
setcolor(t, 1)
for i in range(4):
t.color(randint(0,255),randint(0,255),randint(0,255))
t.begin_fill()
t.fd(100)
t.lt(120)
t.fd(100)
t.lt(120)
t.fd(100)
t.lt(120)
t.fd(100)
otto.end_fill()
otto.shape('turtle')
repeat_triangle(otto, 80)
turtle.mainloop()
turtle.bye()
Otto is the name of my turtle. setcolor is a function I wrote to assign random colors. Also, could you tell me how to draw a 3x3 pile of triangles? Thanks a lot. I'm using jupyter notebooks so it might have some differences to regular Python. Image reference can be found here!
Another example of better living through stamping instead of drawing:
from turtle import Screen, Turtle
from random import random
TRIANGLE_EDGE = 100
CURSOR_EDGE = 20
TRIANGLE_HEIGHT = TRIANGLE_EDGE * 3 ** 0.5 / 2
def repeat_triangle(turtle, repetitions):
for _ in range(repetitions):
turtle.color(random(), random(), random())
turtle.stamp()
turtle.forward(TRIANGLE_HEIGHT)
screen = Screen()
otto = Turtle('triangle', visible=False)
otto.penup()
otto.setheading(90)
otto.shapesize(TRIANGLE_EDGE / CURSOR_EDGE)
repeat_triangle(otto, 4)
screen.mainloop()
Also, this code may be incorrect, depending on which variant of turtle you're using:
t.color(randint(0,255),randint(0,255),randint(0,255))
The turtle that comes with Python defaults to a float from 0 to 1 -- if you want to use an int from 0 to 255 you have to request such via:
turtle.colormode(255)
A simple rework of your drawing code to stack the triangles could be:
from turtle import Screen, Turtle
from random import randint
def repeat_triangle(t, length):
height = length * 3 ** 0.5 / 2
for _ in range(4):
t.color(randint(0, 255), randint(0, 255), randint(0, 255))
t.begin_fill()
for _ in range(3):
t.fd(length)
t.lt(120)
t.end_fill()
t.sety(t.ycor() + height)
screen = Screen()
screen.colormode(255)
otto = Turtle('turtle')
otto.penup()
repeat_triangle(otto, 100)
screen.mainloop()
You can try this perhaps:
import turtle
import math
from random import randint
otto = turtle.Turtle()
def repeat_triangle(t, l):
for i in range(3):
t.color(randint(0,255),randint(0,255),randint(0,255))
t.begin_fill()
t.fd(100)
t.lt(120)
t.fd(100)
t.lt(120)
t.fd(100)
t.lt(120)
t.fd(100)
#added code starts here
t.lt(180) #turn 180 (reverse direction)
t.fd(50) #go halfway
t.lt(60) #turn downwards and start drawing
t.fd(100)
t.lt(120)
t.fd(100) #finishing after the loop
t.lt(120)
t.fd(100)
#added code finishes here
otto.end_fill()
otto.shape('turtle')
repeat_triangle(otto, 80)
turtle.mainloop()
turtle.bye()