Reputation: 31
I am wondering how to get a random color out of a list to use in the draw_rectangle()
colors = ["red", "orange", "yellow", "green", "blue", "violet"]
canvas.create_rectangle(self.x, self.y, self.x + 60, self.y + 60, fill = random.choice(colors))
This causes my code to crash, what else can I try?
Upvotes: 2
Views: 23689
Reputation: 239443
You can use random.choice
like this
import random
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
canvas.create_rectangle(self.x, self.y, self.x + 60, self.y + 60, fill = random.choice(colors))
This will pass a random color to fill
whenever this code is executed.
Upvotes: 3
Reputation: 179
de=("%02x"%random.randint(0,255))
re=("%02x"%random.randint(0,255))
we=("%02x"%random.randint(0,255))
ge="#"
color=ge+de+re+we
and in tkinter put
fill=color
easy you can also make
fill="#"+("%06x"%random.randint(0,16777215))
Upvotes: 5
Reputation: 2233
You can use choice, from package random
random.choice(color)
Upvotes: -1