Python(TKinter)中形状的随机填充颜色

Shl*_*pas 2 python random tkinter list

我想知道如何从列表中获取随机颜色以在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))
Run Code Online (Sandbox Code Playgroud)

这导致我的代码崩溃,我还能尝试什么?

Mam*_*zur 5

de=("%02x"%random.randint(0,255))
re=("%02x"%random.randint(0,255))
we=("%02x"%random.randint(0,255))
ge="#"
color=ge+de+re+we
Run Code Online (Sandbox Code Playgroud)

并在 tkinter 中输入

fill=color
Run Code Online (Sandbox Code Playgroud)

简单你也可以做

fill="#"+("%06x"%random.randint(0,16777215))
Run Code Online (Sandbox Code Playgroud)