title: How to change a circles color in python, using iteration of a few selected colors, which changes with each button press, using Tkinter in python?
body: <p>I'm trying to design a <strong>Mastermind</strong> game, however I cannot seem to find a way to alter a circles color after it has been defined, in which it changes color each time a button is pressed, and iterates through a given list of colors. I also am looking for a way to store these colors within a list to be used later, and compared to a correct list of color circles.</p>
  
  <pre><code>import Tkinter as tk
  import Tkinter
  import math
  import random
  from Tkinter import *
  
  class ResizingCanvas(Canvas):
      def __init__(self,parent,**kwargs):
          Canvas.__init__(self,parent,**kwargs)
          self.bind("&lt;Configure&gt;", self.on_resize)
          self.height = self.winfo_reqheight()
          self.width = self.winfo_reqwidth()
  
      def on_resize(self,event):
          wscale = float(event.width)/self.width
          hscale = float(event.height)/self.height
          self.width = event.width
          self.height = event.height
          self.config(width=self.width, height=self.height)
          self.scale("all",0,0,wscale,hscale)
  
  def main():
  
      root = Tk()
  
      myframe = Frame(root)
      myframe.pack(fill=BOTH, expand=YES)
      mycanvas = ResizingCanvas(myframe,width=300, height=425, highlightthickness=0)
      mycanvas.pack(fill=BOTH, expand=YES)
  
  
      root.title('MasterMind!')
  
      dash_seperation_line = mycanvas.create_line(195,350,195,35, dash =(3,5))
  
      top_line = mycanvas.create_line(0,35,300,35)
  
      blue_layer = mycanvas.create_rectangle(0,425,425,325,fill = 'blue')
  
      title_text = mycanvas.create_text(150,15,text='MasterMind!')
  
      colors = ['red','green','blue']
  
  
      #Trying to cycle through these given colors
      color_iteration = color=itertools.cycle(('blue', 'green', 'orange', 'red', 'yellow'))
  
      circles_list1 = []
      circles_list2 = []
      circles_list3 = []
      circles_list4 = []
      circles_list5 = []
      circles_list6 = []
  
      rectangles_list1 = []
      rectangles_list2 = []
      rectangles_list3 = []
      rectangles_list4 = []
      rectangles_list5 = []
      rectangles_list6 = []
  
      random_pick1 = random.choice(colors)
      random_pick2 = random.choice(colors)
      random_pick3= random.choice(colors)
      random_pick4 = random.choice(colors)
  
      answer_key = [random_pick4,random_pick3,random_pick2,random_pick1]
  
      r = 10
      x = 300
      y = 300
  
      #CIRCLES***
      for x in range(30,220,48):
          y = 50
          circle1 = mycanvas.create_oval(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          circles_list1.append(circle1)
      c1_1 =mycanvas.itemconfig(circles_list1[0])
      c2_1 =mycanvas.itemconfig(circles_list1[1])
      c3_1 =mycanvas.itemconfig(circles_list1[2])
      c4_1 =mycanvas.itemconfig(circles_list1[3])
  
  
      for x in range(30,220,48):
          y = 100
          circle2 = mycanvas.create_oval(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          circles_list2.append(circle2)
      c1_2 =mycanvas.itemconfig(circles_list2[0])
      c2_2 =mycanvas.itemconfig(circles_list2[1])
      c3_2 =mycanvas.itemconfig(circles_list2[2])
      c4_2 =mycanvas.itemconfig(circles_list2[3])
  
      for x in range(30,220,48):
          y = 150
          circle3 = mycanvas.create_oval(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          circles_list3.append(circle3)
      c1_3 =mycanvas.itemconfig(circles_list3[0])
      c2_3 =mycanvas.itemconfig(circles_list3[1])
      c3_3 =mycanvas.itemconfig(circles_list3[2])
      c4_3 =mycanvas.itemconfig(circles_list3[3])
  
      for x in range(30,220,48):
          y = 200
          circle4 = mycanvas.create_oval(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          circles_list4.append(circle4)
      c1_4 =mycanvas.itemconfig(circles_list4[0])
      c2_4 =mycanvas.itemconfig(circles_list4[1])
      c3_4 =mycanvas.itemconfig(circles_list4[2])
      c4_4 =mycanvas.itemconfig(circles_list4[3])
  
      for x in range(30,220,48):
          y = 250
          circle5 = mycanvas.create_oval(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          circles_list5.append(circle5)
      c1_5 =mycanvas.itemconfig(circles_list5[0])
      c2_5 =mycanvas.itemconfig(circles_list5[1])
      c3_5 =mycanvas.itemconfig(circles_list5[2])
      c4_5 =mycanvas.itemconfig(circles_list5[3])
  
      for x in range(30,220,48):
          y = 300
          circle6 = mycanvas.create_oval(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          circles_list6.append(circle6)
      c1_6 =mycanvas.itemconfig(circles_list6[0])
      c2_6 =mycanvas.itemconfig(circles_list6[1])
      c3_6 =mycanvas.itemconfig(circles_list6[2])
      c4_6 =mycanvas.itemconfig(circles_list6[3],fill = current_color)
  
  
      #RECTANGLES***
      for x in range(210,300,25):
          y = 50
          rectangle1 = mycanvas.create_rectangle(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          rectangles_list1.append(rectangle1)
      r1_1 =mycanvas.itemconfig(rectangles_list1[0])
      r2_1 =mycanvas.itemconfig(rectangles_list1[1])
      r3_1 =mycanvas.itemconfig(rectangles_list1[2])
      r4_1 =mycanvas.itemconfig(rectangles_list1[3])
      for x in range(210,300,25):
          y = 100
          rectangle2 = mycanvas.create_rectangle(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          rectangles_list2.append(rectangle2)
      r1_2 =mycanvas.itemconfig(rectangles_list2[0])
      r2_2 =mycanvas.itemconfig(rectangles_list2[1])
      r3_2 =mycanvas.itemconfig(rectangles_list2[2]) 
      r4_2 =mycanvas.itemconfig(rectangles_list2[3])  
      for x in range(210,300,25):
          y = 150
          rectangle3 = mycanvas.create_rectangle(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          rectangles_list3.append(rectangle3)
      r1_3 =mycanvas.itemconfig(rectangles_list3[0])
      r2_3 =mycanvas.itemconfig(rectangles_list3[1])
      r3_3 =mycanvas.itemconfig(rectangles_list3[2])
      r4_3 =mycanvas.itemconfig(rectangles_list3[3])
      for x in range(210,300,25):
          y = 200
          rectangle4 = mycanvas.create_rectangle(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          rectangles_list4.append(rectangle4)
      r1_4 =mycanvas.itemconfig(rectangles_list4[0])
      r2_4 =mycanvas.itemconfig(rectangles_list4[1])
      r3_4 =mycanvas.itemconfig(rectangles_list4[2])
      r4_4 =mycanvas.itemconfig(rectangles_list4[3])
      for x in range(210,300,25):
          y = 250
          rectangle5 = mycanvas.create_rectangle(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          rectangles_list5.append(rectangle5)
      r1_5 =mycanvas.itemconfig(rectangles_list5[0])
      r2_5 =mycanvas.itemconfig(rectangles_list5[1])
      r3_5 =mycanvas.itemconfig(rectangles_list5[2])
      r4_5 =mycanvas.itemconfig(rectangles_list5[3])
      for x in range(210,300,25):
          y = 300
          rectangle6 = mycanvas.create_rectangle(x-r, y-r, x+r, y+r, outline='#000000',fill='white')
          rectangles_list6.append(rectangle6)
      r1_6 =mycanvas.itemconfig(rectangles_list6[0])
      r2_6 =mycanvas.itemconfig(rectangles_list6[1])
      r3_6 =mycanvas.itemconfig(rectangles_list6[2])
      r4_6 =mycanvas.itemconfig(rectangles_list6[3])
  
      print(answer_key)
  
  
  
  
      enter_button = Button(root,text='Enter')
  
      B1 = Button(root,text='B1') # Use these buttons to change each column of circles
      B2 = Button(root,text='B2')
      B3 = Button(root,text='B3')
      B4 = Button(root,text='B4')
  
      enter_button.place(relx=1, x=-125, y=380, anchor=NE)
  
      B1.place(relx=1, x=-246, y=325, anchor=NE)
      B2.place(relx=1, x=-199, y=325, anchor=NE)
      B3.place(relx=1, x=-150, y=325, anchor=NE)
      B4.place(relx=1, x=-101, y=325, anchor=NE)
  
  
      mycanvas.addtag_all("all")
      root.mainloop()
  
  if __name__ == "__main__":
      main()
  </code></pre>