0

hi everyone i wanna create a class in tkinter but im finding it very difficult to begin it as im new to python. i want a class traffic light that will operate continuously (that is red to green to red.... ). thanks..

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by harrykokil
0

This would be a typical Tkinter class creating what looks like a traffic light. I leave it up to you to assign the proper light actions.

# a simple Tkinter class

import Tkinter as tk

class TrafficLight(tk.Frame):
    """inherits tk.Frame"""
    def __init__(self, parent=None):
        tk.Frame.__init__(self, parent, bg='green')
        self.grid()
        # create a canvas to draw on
        self.cv = tk.Canvas(self, width=260, height=280, bg='white')
        self.cv.grid()

        self.make_widgets()

    def make_widgets(self):
        self.cv.create_rectangle(80, 20, 170, 260)
        # imagine a square box with
        # upper left corner coordinates x1, y1
        # lower right corner coordinates x2, y2
        # and sides of length span for each circle
        span = 50
        x1 = 100
        y1 = 50
        x2 = x1 + span
        y2 = y1 + span
        self.cv.create_oval(x1, y1, x2, y2, fill='red')
        self.cv.create_oval(x1, y1+70, x2, y2+70, fill='yellow')
        self.cv.create_oval(x1, y1+140, x2, y2+140, fill='green')


if __name__ == '__main__':
    light = TrafficLight()
    light.mainloop()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.