使用 random.choice 时出现元组索引超出范围错误
Tuple Index Out of Range Error when using random.choice
在尝试创建随机大小的矩形时,我似乎 运行 进入了这个 "tuple index out of range error"。以前,我只将颜色设置为随机并且有效。但是,当我尝试将坐标设置为随机时,出现错误并且 canvas 上没有任何内容。有什么我想念的吗?
from tkinter import *
root = Tk()
import random
canvas = Canvas(root, width=550, height=400)
canvas.pack()
coord = [30, 40, 130, 140]
color = ["red", "orange", "yellow", "green", "blue", "violet"]
for j in range(10):
rectangle = canvas.create_rectangle(x0 = random.choice(coord) +j*5,
y0 = random.choice(coord)+j*5,
x1 = random.choice(coord)+j*5,
y1 = random.choice(coord)+j*5,
outline="black",
fill = random.choice(color))
删除 x0=, y0= ...
for j in range(10):
rectangle = canvas.create_rectangle(random.choice(coord) +j*5,
random.choice(coord)+j*5,
random.choice(coord)+j*5,
random.choice(coord)+j*5,
outline="black",
fill = random.choice(color))
在尝试创建随机大小的矩形时,我似乎 运行 进入了这个 "tuple index out of range error"。以前,我只将颜色设置为随机并且有效。但是,当我尝试将坐标设置为随机时,出现错误并且 canvas 上没有任何内容。有什么我想念的吗?
from tkinter import *
root = Tk()
import random
canvas = Canvas(root, width=550, height=400)
canvas.pack()
coord = [30, 40, 130, 140]
color = ["red", "orange", "yellow", "green", "blue", "violet"]
for j in range(10):
rectangle = canvas.create_rectangle(x0 = random.choice(coord) +j*5,
y0 = random.choice(coord)+j*5,
x1 = random.choice(coord)+j*5,
y1 = random.choice(coord)+j*5,
outline="black",
fill = random.choice(color))
删除 x0=, y0= ...
for j in range(10):
rectangle = canvas.create_rectangle(random.choice(coord) +j*5,
random.choice(coord)+j*5,
random.choice(coord)+j*5,
random.choice(coord)+j*5,
outline="black",
fill = random.choice(color))