0

Im designing a Mastermind game, and so far have come up with a circle that changes color with iterating through a list of colors with the press of a button:

set_up_answer = []

color_iteration1 = itertools.cycle(('blue', 'green', 'orange', 'red','yellow'))

def callback1():
    mcircle1 = mycanvas.create_oval(10,620,86,675,outline='#000000',fill=next(color_iteration1))
    res1 = mycanvas.itemcget(mcircle1, 'fill')
    set_up_answer.append(res1)

B1 = Button(root,text='B1',command =callback1)

Keep in mind this is not complete code***, and what happens is that if i go through the iterate list by pressing the button multiple times, end up with say the circle containing the fill 'orange' and then add it to (set_up_answer) to compare to other circles, what it returns is something like ['blue', 'green', 'orange'], meaning it returns every color in the iterate list before what the actual fill is as well as the actual fill color, how do I limit the value returning the fill to just the specific color of the iterate list it actually is?

3
  • Are you wanting set_up_answer to only have one value? Are you using a list as a global so that you can modify it within the function? You could just have global set_up_answer in the function , then you can assign to it. This isn't ideal, but will work. Commented Mar 24, 2016 at 0:01
  • Peter, no I am using a list because I am planning to add multiple more circles, in which each will add their fill color to create a list(set_up_answer) of a set of different colors, to then be compared to an answer key(different list) that has randomly generated colors, and if they are the same to return true. Commented Mar 24, 2016 at 0:07
  • Each time you press the button it appends the colour to the list. Isn't that what you want? Commented Mar 24, 2016 at 8:15

1 Answer 1

1

How about:

def callback1():
    color = next(color_iteration1)
    mcircle1 = mycanvas.create_oval(10,620,86,675,outline='#000000',fill=color)
    set_up_answer.append(color)

Using mycanvas.itemcget seems very roundabout and might be the cause of your problem (I don't know tkinter so I can't tell).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.