简体   繁体   中英

How to make all of my for loop runs at the same time

I have few for loop that will flash objects for 5 seconds with 0.5 seconds interval. And I am trying to figure out how to make all these object to flash at the same time, in other words, I want to run all the for loops at the same time.

Need help!

        count1=5

        for k in range (count1):
            error.setFill('grey')
            time.sleep(.50)
            error.setFill('orange')
            time.sleep(.50)

        for l in range (count1):              
            go.setFill('red')
            time.sleep(.50)
            go.setFill('green')
            time.sleep(.50)

        for m in range (count1):               
            warning.setFill('black')
            time.sleep(.50)
            warning.setFill('red')
            time.sleep(.50)

        for u in range (count1):
            Verify.setFill('green')
            time.sleep(.50)
            Verify.setFill('grey')
            time.sleep(.50)

        for w in range (count1):
            Launch.setFill('red')
            time.sleep(.50)
            Launch.setFill('green')
            time.sleep(.50)

This is a simple way to do it, supposing that setFill() is fast enough so they appear to be turning on and off at the same time.

count1=5

for k in range (count1):
    # Turn on
    error.setFill('grey')
    go.setFill('red')
    warning.setFill('black')
    Verify.setFill('green')
    Launch.setFill('red')
    time.sleep(.50)
    # Turn off
    error.setFill('orange')
    go.setFill('green')
    warning.setFill('red')
    Verify.setFill('grey')
    Launch.setFill('green')
    time.sleep(.50)

In another case, you will need the create a new thread for each light.

Seeing as all your loops are all running in the range of count1 , why not run all commands in the same loop, for example:

    count1=5

    for n in range (count1):
    # first line of each loop
        error.setFill('grey')
        go.setFill('red')
        warning.setFill('black')
        Verify.setFill('green')
        Launch.setFill('red')
    # pause and then third line of each loop
        time.sleep(.50) 
        error.setFill('orange')
        go.setFill('green')         
        warning.setFill('red')
        Verify.setFill('grey')
        Launch.setFill('green')
        time.sleep(.50)

This is then running all your loops at once, or rather in the same loop, and it takes less effort and typing. Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM