如何通过for循环为每个模块添加不同的颜色?喜欢在下面评论吗?

时间:2019-12-11 00:47:29

标签: python-3.x

如何为每个模块添加颜色?我尝试使用

self.color ={"red","green","yellow","blue","agua","orange"}

填写,但这无效

    for i in self.__pop_percentage.values():
        self.extent = (i/self.__sum_pop_percentage*360)
        self.canvas.create_arc(400,400,100,100,start=self.start,extent=(self.extent),fill="red")
        print(i)
        self.start = self.start + self.extent
        self.count = self.count +1
    self.canvas.pack()

    #self.canvas.create_arc(400,400,100,100,start=0,extent=130.212,fill="red")
    #self.canvas.create_arc(400,400,100,100,start=130.212,extent=121.068,fill="green")
    #self.canvas.create_arc(400,400,100,100,start=251.28,extent=19.332,fill="yellow")
    #self.canvas.create_arc(400,400,100,100,start=270.612,extent=17.996,fill="blue")
    #self.canvas.create_arc(400,400,100,100,start=288.608,extent=14.9,fill="aqua")
    #self.canvas.create_arc(400,400,100,100,start=303.508,extent=56.492,fill="orange")

    #self.canvas.pack()

1 个答案:

答案 0 :(得分:0)

您可以定义颜色列表,然后从该列表中选择下一个颜色:

colors = ["red", "green", "yellow", "blue", "aqua", "orange"]
for color_index, i in enumerate(self.__pop_percentage.values()):
    ...
    self.canvas.create_arc(..., fill=colors[color_index])

请注意,如果您的值大于颜色数量,则此操作将失败。在这种情况下,您可以循环颜色:

    self.canvas.create_arc(..., fill=colors[color_index % len(colors)])