I am studying Python Turtle. I know that the color mode should be 255
in order to use RGB mode. I also know that the correct format is
turtle.dot(int,""). But I get the error:
TurtleGraphicsError: bad color arguments: None
How can I implement RGB?
from turtle import Turtle, Screen
import turtle as t
import random
tim = t.Turtle()
t.colormode(255)
# t.colormode(255)
def rndmclr():
r=random.randrange(255)
g=random.randrange(255)
b=random.randrange(255)
t.pencolor((r,g,b))
t.speed("fastest")
def tendots():
for i in range(10):
t.dot(10,rndmclr())
t.penup()
t.fd(30)
t.pendown()
t.fd(0)
for i in range(10):
tendots()
t.left(90)
t.penup()
t.fd(10)
t.pendown()
t.left(90)
tendots()
t.right(90)
t.penup()
t.fd(10)
t.pendown()
t.right(90)
screen = Screen()
screen.exitonclick()
Your primary flaw is here:
def rndmclr():
r=random.randrange(255)
g=random.randrange(255)
b=random.randrange(255)
t.pencolor((r,g,b))
# ...
t.dot(10,rndmclr())
The rndmclr() sets the random color when it should return the color as it's being used as an argument to dot() where it is currently returning None as the color to use. There are other flaws in your program, most significanly the double import of turtle that has you using both the object-oriented API and the functional API which only leads to trouble -- pick one and stick with it:
from turtle import Turtle, Screen
from random import randrange
def rndmclr():
r = randrange(255)
g = randrange(255)
b = randrange(255)
return (r, g, b)
def tendots():
for _ in range(10):
turtle.dot(10, rndmclr())
turtle.penup()
turtle.forward(30)
turtle.pendown()
screen = Screen()
screen.colormode(255)
turtle = Turtle()
turtle.speed('fastest')
for _ in range(10):
tendots()
turtle.left(90)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.left(90)
tendots()
turtle.right(90)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.right(90)
screen.exitonclick()
I am new to turtle and I am trying to draw two hearts with an arrow. But after I finished two hearts, it does not draw anymore. I already have the "pendown" and I don't know why it does not draw.
I have my codes here. Thank you for your help. ♡♡
# draw a big heart
turtle.pencolor('pink')
turtle.fillcolor('pink')
turtle.begin_fill()
turtle.left(90)
turtle.circle(100, 200)
turtle.left(20)
turtle.forward(285)
turtle.left(100)
turtle.forward(285)
turtle.left(20)
turtle.circle(100, 200)
turtle.end_fill()
turtle.penup()
turtle.left(90)
turtle.forward(150)
turtle.pendown
#draw a small heart
turtle.pencolor('red')
turtle.fillcolor('red')
turtle.begin_fill()
turtle.left(90)
turtle.circle(80, 200)
turtle.left(20)
turtle.forward(228)
turtle.left(100)
turtle.forward(228)
turtle.left(20)
turtle.circle(80, 200)
turtle.end_fill()
#draw an arrow
turtle.pencolor('black')
turtle.pensize(5)
turtle.penup()
turtle.right(80)
turtle.forward(400)
turtle.right(180)
turtle.pendown
turtle.forward(600)
I don't know what turtle is but you write turtle.penup() but turtle.pendown. So this may be the problem
I am trying to move the square to right and draw 36 squares to make a circle out of it:
def draw_art(x,y):
print("Started the op")
window = turtle.Screen()
window.bgcolor("blue")
print("start the drwaing")
c =0
brad = turtle.Turtle()
brad.shape("turtle")
brad.color("green")
brad.speed(3)
print("enter loop")
for i in range(1,37):
draw_square(x,y)
brad.right(10)
window.exitonclick()
draw_art(200,90)
This might get you closer to what you're trying to do. It defines the missing draw_square() function and then draws 36 squares in a circle:
from turtle import Screen, Turtle
def draw_square(turtle):
for _ in range(4):
turtle.forward(50)
turtle.right(90)
def draw_art(turtle, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for _ in range(36):
draw_square(turtle)
turtle.right(10)
print("Started the app")
window = Screen()
window.bgcolor('blue')
brad = Turtle()
brad.shape('turtle')
brad.color('green')
brad.speed('fastest') # because I have no patience
print("Start the drawing")
draw_art(brad, 200, 90)
brad.hideturtle()
window.exitonclick()
I have created the wheel, but when I try to make code to spin it will not work.
I have already tried to make it using a loop but that was near impossible for me. I am basically drawing the wheel over. Here is some code from the spinning wheel part:
turtle.listen()
if turtle.onkeypress("space"):
colors = ['#880000','#884400','#884400','#888800',
'#888800','#008800','#008800','#008800',
'#008800','#008800','#008888','#008888',
'#008888','#008888','#008888','#000088',
'#000088','#000088','#000088','#000088']
for color in colors:
slice_angle = 360 / len(colors)
heading, position = 90, (center[0] + radius, center[1])
turtle.color(color, color)
turtle.speed(0)
turtle.penup()
turtle.goto(position)
turtle.setheading(heading)
turtle.pendown()
turtle.begin_fill()
turtle.circle(radius, extent=slice_angle)
heading, position = turtle.heading(), turtle.position()
turtle.penup()
turtle.goto(center)
turtle.end_fill()
turtle.penup()
time.sleep(0.2)
colors = ['#884400','#884400','#888800',
'#888800','#008800','#008800','#008800',
'#008800','#008800','#008888','#008888',
'#008888','#008888','#008888','#000088',
'#000088','#000088','#000088','#000088','#880000']
for color in colors:
slice_angle = 360 / len(colors)
heading, position = 90, (center[0] + radius, center[1])
turtle.color(color, color)
turtle.speed(0)
turtle.penup()
turtle.goto(position)
turtle.setheading(heading)
turtle.pendown()
turtle.begin_fill()
turtle.circle(radius, extent=slice_angle)
heading, position = turtle.heading(), turtle.position()
turtle.penup()
turtle.goto(center)
turtle.end_fill()
turtle.penup()
time.sleep(0.2)
The code keeps on going to make the wheel 'spin'.
This is what I get:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 701, in eventfun
fun()
TypeError: 'str' object is not callable
My impression is that you're not trying to make the wheel spin but rather make it look like it's spinning by cycling its colors. Here's my example of doing such which uses tracer() and update() to turn off drawing while remaking the circle with the colors shifted. It uses a timer event to trigger redraws. Rather than your fixed list of colors, I'm going to use continuous hues, but you should be able to use any colors you wish:
from turtle import Screen, Turtle
from colorsys import hsv_to_rgb
RADIUS = 100
NUMBER_OF_WEDGES = 20
SLICE_ANGLE = 360 / NUMBER_OF_WEDGES
screen = Screen()
screen.tracer(False)
turtle = Turtle(visible=False)
turtle.penup()
center = turtle.position()
turtle.sety(turtle.ycor() - RADIUS)
hues = [color / NUMBER_OF_WEDGES for color in range(NUMBER_OF_WEDGES)] # precompute hues
index = 0
def draw_circle():
global index
for hue in range(NUMBER_OF_WEDGES):
turtle.color(hsv_to_rgb(hues[(hue + index) % NUMBER_OF_WEDGES], 1.0, 1.0))
turtle.pendown()
turtle.begin_fill()
turtle.circle(RADIUS, extent=SLICE_ANGLE)
position = turtle.position()
turtle.goto(center)
turtle.end_fill()
turtle.penup()
turtle.goto(position)
screen.update()
index = (index + 1) % NUMBER_OF_WEDGES
screen.ontimer(draw_circle, 40)
draw_circle()
screen.exitonclick()
This works, but, on my system, inexplicably slows down over time.
Let's try a different approach that doesn't redraw anything at the Python level during the cycling of colors. We're going to design a new cursor shape, "wedge" and build our circle out of turtles! All the wedges will be prepositioned and not move nor be redrawn. The timer event handler will simply ask each turtle to take on the color of its neighbor:
from turtle import Screen, Turtle
from colorsys import hsv_to_rgb
RADIUS = 100
NUMBER_OF_WEDGES = 20
SLICE_ANGLE = 360 / NUMBER_OF_WEDGES
screen = Screen()
screen.tracer(False)
# create a pie wedge-shaped cursor
turtle = Turtle(visible=False)
turtle.begin_poly()
turtle.sety(turtle.ycor() - RADIUS)
turtle.circle(RADIUS, extent=SLICE_ANGLE)
turtle.home()
turtle.end_poly()
screen.register_shape("wedge", turtle.get_poly())
# create a turtle for each wedge in the pie
turtles = []
for hue in range(NUMBER_OF_WEDGES):
turtle = Turtle("wedge")
turtle.color(hsv_to_rgb(hue / NUMBER_OF_WEDGES, 1.0, 1.0))
turtle.setheading(hue * SLICE_ANGLE)
turtles.append(turtle)
def draw_circle():
# have each turtle take on the color of its neighbor
for index, turtle in enumerate(turtles):
turtle.color(*turtles[(index + 1) % NUMBER_OF_WEDGES].color())
screen.update()
screen.ontimer(draw_circle, 40)
draw_circle()
screen.exitonclick()
Notice how much simpler our main loop, draw_circle(), is and that the spinning doesn't slow down.
This code draws a few letters using turtle graphics:
import turtle
turtle.speed(1)
myWin = turtle.Screen()
turtle.left(90)
turtle.forward(260)
turtle.left(55)
turtle.forward(-60)
turtle.right(55)
turtle.forward(-60)
turtle.right(55)
turtle.forward(-60)
turtle.left(100)
turtle.forward(-60)
turtle.right(45)
turtle.forward(-60)
turtle.right(55)
turtle.forward(-50)
turtle.right(35)
turtle.forward(100)
turtle.left(90)
turtle.forward(260)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(50)
turtle.forward(-200)
turtle.right(130)
turtle.forward(50)
turtle.left(45)
turtle.forward(260)
myWin.exitonclick()
The problem is it starts drawing in the middle of the screen and I want it to start at the far left side. Is there any way to change the initial position
Note that the turtle interface is a coordinate grid.
so you can use the goto(x, y) function
turtle.penup()
turtle.goto(300,200)
turtle.pendown()
Use turtle.penup to pick up the pen and move to the position you want, and then do turtle.pendown