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)

Dani AI

Generated

You are on the right track, . With Zelle’s graphics.py you generally need to flush drawing changes before pausing. If you call time.sleep(5) right after setFill(), the UI may not repaint until the sleep ends. Call the library’s top-level update() to force a refresh, or create the window with autoflush=False and update explicitly each step. Also, Tk’s named colors include "red", "yellow", and "green", but not "amber". If you want an amber-like hue, build one with color_rgb(r,g,b) (for example, 255,191,0). See the docs on display updates and colors for details. (mcsp.wartburg.edu)

Small, tested pattern you can drop in. Note the explicit update() and a helper that turns other lamps off before lighting the next one. This avoids the UnboundLocalError you hit when color was never defined, and it ensures exactly one lamp is lit at a time.

from graphics import *
import time

def traffic_lights():
    win = GraphWin("Traffic", 200, 200, autoflush=False)
    red = Circle(Point(100, 50), 20).draw(win)
    yellow = Circle(Point(100, 100), 20).draw(win)
    green = Circle(Point(100, 150), 20).draw(win)

    AMBER = color_rgb(255, 191, 0)  # optional: use "yellow" instead

    def show(light, fill):
        for c in (red, yellow, green):
            c.setFill("black")
        light.setFill(fill)
        update()  # push changes to the window

    while True:
        show(red, "red");      time.sleep(5)
        show(yellow, "yellow"); time.sleep(5)  # or AMBER
        show(green, "green");  time.sleep(5)

Side note for : current graphics.py v5 works on both Python 2.x and 3.x and adds the top-level update() exactly for scenarios like this. (mcsp.wartburg.edu)

Recommended Answers

All 13 Replies

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

Member Avatar for Member #562630

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

Member Avatar for Member #562630

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 Member #562630

what's the error message?

sorry i forgot to mention.. its

Sorry, I am using Python 3.1.1 and 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\", line 19, in trafficLights
light.setFill( color )
UnboundLocalError: local variable 'color' referenced before assignment

Member Avatar for Member #562630

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 Member #562630

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

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

This little module has been updated to work with Python2 and Python3 recently, look at the new beta version at:

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.