1

Suppose I have a list of five different colours, colour = ['red', 'blue', ...] etc. I then want to create a loop for making an arbitrary amount of circles defined by the tkinter modules. That would mean something like

def Circles():
    y = 0
    x = 0
    while y <= 900 and x <= 900:
        x = x + 100
        y = y + 100
        w.create_oval(x, y, 0, 0, fill='red')

How would I include a loop for that fill='red' part, where I instead want fill=colour[N], where N would change in a loop? So the first circle would be red, next blue, next green etc? I also know that these ones overlap and I will try to make them not overlap, but that's not the question here.

3
  • 1
    Since x and y are the same, use 1 variable and use a for loop: for x in range(0,1000,100): Commented Mar 10, 2017 at 15:10
  • Also as an aside, you have a list of colors, definitely not an array. Commented Mar 10, 2017 at 15:11
  • Hello Edwin and welcome to the site. We like to keep questions to the point here, so I trimmed yours down a bit. Feel free to edit again if you'd like. Commented Mar 10, 2017 at 15:12

4 Answers 4

3

I would use itertools.cycle. Also as you increment both x and y by 100, up to 900, use range.

from itertools import cycle
colors = ['red', 'blue', 'yellow', 'green', 'orange']

def Circles():
    cycling_colors = cycle(colors)
    for i in range(0, 901, 100):
        w.create_oval(i, i, 0, 0, fill=next(cycling_colors))
Sign up to request clarification or add additional context in comments.

3 Comments

It's better to use next(cycling_colors)... (changes between 2.x and 3.x semantics) - next(...) will work on both
I'd probably also be tempted to do for i, c in zip(range(0, 901, 100), cycle(colors)): w.create_oval(i, i, 0, 0, fill=c) but given we don't know if it's 2.x or 3.x, on 2.x you'd definitely need izip or else you'd never exit the loop :)
Thank you! I will try to make sense of the syntax of itertools!
1

You can use this way, if you want to make nested loop:

colours = ['red', 'blue', 'green']

def Circles():
    y = 0
    x = 0
    while y <= 900 and x <= 900:
        x = x + 100
        y = y + 100
        for color in colours:
            w.create_oval(x, y, 0, 0, fill=colour)

Circles()

Comments

0
def Circles():
    y = 0
    x = 0
    while y <= 900 and x <= 900:
        x = x + 100
        y = y + 100
        #iterate the colour list
        for colo in colour:
            w.create_oval(x, y, 0, 0, fill=colo) 

Hope it helps!

Comments

0

I assume you may want to iterate over the plane of x an y. As for color, agreed, cycle is the right approach for the colors.

from itertools import cycle
def gen():
   color_cyclist = cycle('red', 'blue', 'green')
   for x in range(0,1000, 100):
       for y in range(0, 1000, 100):
           yield x, y, next(color_cyclist)

for x, y, color in gen():
    w.create(x, y, 0, 0, fill=color)

1 Comment

I tried that for x and for y loop on my own, before I came here. Python crashed when I tried it! Yours works just fine. Seems I need time :)

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.