how could i make the function to do the colours red, yellow and green for 5 sec delays and make the loop keep on going?

def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
    while True:
        import time
        time.sleep(5)

Recommended Answers

All 13 Replies

... and what kind of GUI toolkit is this?

Member Avatar for masterofpuppets

hi,
as Ene Uran mentioned, not sure what GUI toolkit this is but I'll try to help anyway, I think I get the overall idea :)
Just a note: put the import statements in the beginning of the program.

It's something like this:

import time

def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
    
    color = "red"
    while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            if color == "red":
                color = "amber"
            elif color == "amber":
                color = "green"
            elif color == "green":
                color = "red"

Note I think this should work but I'm not sure because I haven't tested it :)

hope this helps :)

... and what kind of GUI toolkit is this?

sorry i forgot to mention.. its graphics.py

Member Avatar for masterofpuppets

sorry I forgot that when you change the color you need to set the color of the previous light to black again:

while True:
    for light in [ red, amber, green ]:
        light.setFill( color )
        # not sure what the update function for the screen is but you need to call it before you call time.sleep()
        time.sleep(5)
        light.setFill( "black" )
        if color == "red":
            color = "yellow"
        elif color == "yellow":
            color = "green"
        elif color == "green":
            color = "red"

you could simplify the if..elif to this

changeColor = { "red":"yellow", "yellow":"green", "green":"red" }
while True:
    for light in [ red, amber, green ]:
        light.setFill( color )
        # not sure what the update function for the screen is but you need to call it before you call time.sleep()
        time.sleep(5)
        light.setFill( "black" )
        color = changeColor[ color ]

sorry I forgot that when you change the color you need to set the color of the previous light to black again:

while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            light.setFill( "black" )
            if color == "red":
                color = "amber"
            elif color == "amber":
                color = "green"
            elif color == "green":
                color = "red"

i get this error... when i try and run the code...

def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
    while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            light.setFill( "black" )
            if color == "red":
                color = "amber"
            elif color == "amber":
                color = "green"
            elif color == "green":
                color = "red"
Member Avatar for masterofpuppets

what's the error message?

sorry i forgot to mention.. its graphics.py

Sorry, I am using Python 3.1.1 and graphics.py does not work with Python3.

what's the error message?

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
trafficLights()
File "C:\Documents and Settings\Compaq_Owner\My Documents\Python\week08.py", line 19, in trafficLights
light.setFill( color )
UnboundLocalError: local variable 'color' referenced before assignment

Member Avatar for masterofpuppets

ah yes I think I haven't defined color :)

try this:

import time
 
def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
 
    color = "red"
    
    changeColor = { "red":"yellow", "yellow":"green", "green":"red" }
    while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            light.setFill( "black" )
            color = changeColor[ color ]
Member Avatar for masterofpuppets

yes this has to work, I found graphics.py and downloaded it and it worked :)

Can u write please all code for a traffic light, vissual also.

You are welcome to do one, or hire a programmer if you need one for real. But learning requires for you to do honest try yourself. Also the rules of Daniweb tells the same. Also you are welcome to start new threads, but do not continue old threads if your answer does not contribute to original messages left unanswered. As it says at bottom:

This question has already been solved: Start a new discussion instead

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.