Random fill colour for shapes in Python(TKinter)
问题 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? 回答1: 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