[Tutor] Traffic Light

anthonym anthonym at att.net
Sat Jan 19 21:31:25 CET 2013


Hello All,

I am new to Python and taking a course now.  One of my exercises is to
create a traffic light using tkinter.   The exercise also requires that I
create radio buttons that allow the user to click on the color and the
corresponding light will come on.

I followed some examples in my book to write the code but get some errors
that I don't understand .  Code and error messages below

Thanks,
Tony

from tkinter import *  # Import tkinter

class Trafficlight:
    def __init__(self):
        window = Tk()    # Create a window
        window.title("Traffic Light")   # Set a title

        # Add three radio buttons to frame1
        frame1 = Frame(window)   # Create and add a frame to window
        frame1.pack()
        self.v1 = IntVar()
        self.v2 = IntVar()
        rbRed = Radiobutton(frame1, text = "Red", bg = "red",
                variable = self.v2,
                value = 1,
                command = self.processRadiobutton)
        rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow",
                variable = self.v2, value = 2,
                command = self.processRadiobutton)
        rbGreen = Radiobutton(frame1, text = "Green", bg = "green",
                variable = self.v2, value = 3,
                command = self.processRadiobutton)

        rbRed.grid(row = 10, column = 1)
        rbYellow.grid(row = 10, column = 2)
        rbGreen.grid(row = 10, column = 3)

        # Add Radio Button process below once I figure that out

        # Place canvas in the window

        self.canvas = Canvas(window, width = 480, height = 480, bg =
"white")
        self.canvas.pack()

    # Display a rectangle

    def displayRect(self):
        self.canvas.create_rectangle(10, 10, 190, tages = "rect")

    # Display a Oval for the top light

    def displayOval(self):
        self.canvas.create_oval(10, 10, 10, 10)

    # Display an Oval for the middle light

    def displayOval(self):
        self.canvas.create_oval(20, 20, 20, 20)

    # Display an Oval for the bottom light

    def displayOval(self):
        self.canvas.create_oval(30, 30, 30, 30)

     # Create an event loop

Trafficlight()

        
        
Error messages

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 02:56:36)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 57, in
<module>
    Trafficlight()
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 16, in
__init__
    command = self.processRadiobutton)
AttributeError: 'Trafficlight' object has no attribute 'processRadiobutton'
>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 57, in
<module>
    Trafficlight()
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 16, in
__init__
    command = self.processRadiobutton)
AttributeError: 'Trafficlight' object has no attribute 'processRadiobutton'
>>> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130119/1e7d9a94/attachment.html>


More information about the Tutor mailing list