from tkinter import *
from random import randint
D = 30 #球的直径
WIDTH = 600
HEIGHT = 400 #画布尺寸
def randomcolor():#生成随机颜色
colorArr = ['1', '2', '3', '4', '5', '6', '7','8','9','A', 'B','C','D','E','F']
color = ""
for i in range(6):
color += colorArr[randint(0,14)]
return "#"+color
root = Tk()
root.title("Random balls")
canvas = Canvas(root, width = WIDTH, height = HEIGHT) #创建画布
canvas.pack()
def drawballs(): #定义画球的函数
canvas.delete("all")
for a in range(10): #画10次
x1 = randint(0,WIDTH-D)#球左上角x坐标
y1 = randint(0,HEIGHT-D)#球左上角y坐标
canvas.create_oval(x1, y1, x1+D, y1+D, fill = randomcolor())
drawballs()
Button(root, text = "Display", command = drawballs).pack()
mainloop()
如果可以的话,麻烦给个采纳
https://www.cnblogs.com/nancyzhu/p/8401572.html 这是双色球。你可以参考一下
您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~
ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓
【电脑端】戳>>> https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】 戳>>> https://mall.csdn.net/item/52471?utm_source=1146287632