Python(TKinter)中形状的随机填充颜色 [英] Random fill colour for shapes in Python(TKinter)

查看:131
本文介绍了Python(TKinter)中形状的随机填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从列表中获取随机颜色以在 draw_rectangle()

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?

推荐答案

您可以使用

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))

只要执行此代码,这就会将随机颜色传递给 fill .

This will pass a random color to fill whenever this code is executed.

这篇关于Python(TKinter)中形状的随机填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆