如果我要创建一个圆圈,并通过单击按钮迭代颜色,如下面的代码所示:
color_iteration1 = itertools.cycle(('blue', 'green', 'orange', 'red', 'yellow'))
color_iteration2 = itertools.cycle(('blue', 'green', 'orange', 'red', 'yellow'))
color_iteration3 = itertools.cycle(('blue', 'green', 'orange', 'red', 'yellow'))
color_iteration4 = itertools.cycle(('blue', 'green', 'orange', 'red', 'yellow'))
def callback1():
mcircle1 = mycanvas.create_oval(10,620,86,675, outline='#000000',fill=next(color_iteration1))
def callback2():
mcircle2 = mycanvas.create_oval(100,620,176,675, outline='#000000',fill = next(color_iteration2))
def callback3():
mcircle3 = mycanvas.create_oval(190,620,266,675, outline='#000000',fill = next(color_iteration3))
def callback4():
mcircle4 = mycanvas.create_oval(280,620,356,675, outline='#000000',fill = next(color_iteration4))
B1 = Button(root,text='B1',command =callback1)
B2 = Button(root,text='B2',command =callback2)
B3 = Button(root,text='B3',command =callback3)
B4 = Button(root,text='B4',command =callback4)
如何存储圆圈在任何给定时间的颜色,包含在迭代列表中。例如,如果我按两次按钮并降落到绿色,我怎么能说mcircle现在是绿色并将其存储为变量?
哦,如果它更容易,是否有一种方法可以遍历圆的每个像素,如果它检测到某个RGB值,它会发出一个标志,将某个变量变成等于RGB值的任何颜色在圈内?
答案 0 :(得分:2)
如果您保留该项目的引用(create_oval
的返回值),则可以使用Canvas.itemcget
获取该项目的选项:
oval = canvas.create_oval(10, 10, 20, 20, fill='red')
canvas.itemcget(oval, 'fill') # => Returns 'red'