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..

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()

thanks a lot.. very helpful..

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.