-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic_lights.py
More file actions
150 lines (127 loc) · 5.47 KB
/
Copy pathtraffic_lights.py
File metadata and controls
150 lines (127 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
""" A graphical interactive program that models traffic lights
Normal traffic light operating procedure changes from;
- red to green,
- green to yellow,
- and yellow to red.
"""
from tkinter import Tk, Canvas
from tkinter.ttk import Button, Frame
from random import choice, randrange
from functools import partial
from custom_modules.traffic_light_object import TrafficLight
class TrafficLightWindow(object):
""" Graphical window that dispalys a traffic light and change button. """
def __init__(self):
""" (TrafficLightWindow) -> TrafficLight
Initialization function.
Creates a Canvas object and uses that to create a TrafficLight object.
Creates a tkinter.ttk Button object, and wires it into the
do_button_press method.
"""
root = Tk() # Create the main window
root.title('Traffic Light') # Set title bar text
# Create widget container
frame = Frame(root) # Create a frame to hold the widgets
frame.pack() # Make the frame fill the entire window
# Create a graphical button and ensure it calls the do_button_press
# method when the user presses it
button = Button(frame, text='Change', command=self.do_button_press)
# Create and add a drawing surface within the window
canvas = Canvas(frame, width=200, height=300)
# Init a traffic light object instance variable
self.light = TrafficLight(50,20,100, canvas)
# Position button in th containing frame's first row, first column, and
# position canvas in the first row, second column (zero origin)
button.grid(row=0, column=0)
canvas.grid(row=0, column=1)
root.mainloop() # Start GUI event loop
def do_button_press(self):
"""(TrafficLightWindow) -> TrafficLight
The window manager calls this function when the user presses
the graphical button.
Method simply delegates its work to the change method of its TrafficLight
instance variable.
"""
self.light.change()
class MultisizeLightsWindow(object):
"""
Graphical window that dispalys multiple traffic lights (different sizes)
and change buttons.
"""
def __init__(self):
""" (MultisizeLightWindow) -> list
"""
root = Tk() # Create the main window
root.title('Multiple Traffic Lights') # Set title bar text
self.lights = [] # Init list of traffic light objects
# The outer frame holds the nine inner frames that each contain
# a canvas and a button
outer_frame = Frame(root)
outer_frame.pack()
# Create nine drawing surfaces within the window
# determine the exact location and size of each light it creates
for i in range(1, 10):
# Place widget: One traffic light object and one button object
f = Frame(outer_frame, borderwidth=2, relief='groove')
# position widget within grid of rows and columns within its parent
f.grid(row=0, column=i)
c = Canvas(f, width=20 * i, height=250)
c.grid(row=0, column=0)
self.lights.append(TrafficLight(5*i, 10, 10 * i, c))
# b = Button(f, text='Change', command=lambda x=i: lights[x - 1].change())
b = Button(
f, text='Change', command=partial(self.do_button_press, i - 1)
)
b.grid(row=1, column=0)
root.mainloop() # Start the GUI event loop
def do_button_press(self, idx):
"""(TrafficLightWindow) -> TrafficLight
The window manager calls this function when the user presses
the graphical button.
Method simply delegates its work to the change method of its TrafficLight
instance variable.
"""
self.lights[idx].change()
class RandomLightsWindow(object):
"""
Graphical window that displays multiple random traffic lights of
different sizes
"""
def __init__(self):
""" (MultisizeLightWindow) -> list
"""
root = Tk() # Create the main window
root.title('Multiple Random Traffic Lights') # Set title bar text
f = Frame(root)
f.pack()
canvas = Canvas(f, width=800, height=600) # Create a drawing surface within the window
color_list = ['red', 'yellow', 'green']
self.lights_list = [] # Init list of traffic light objects
for i in range(50):
self.lights_list.append(TrafficLight(
randrange(2, 700),
randrange(2, 400),
randrange(5, 120),
canvas,
choice(color_list)
))
# Create a graphical button and ensure it calls the do_button_press
# function when the user presses it
button = Button(f, text='Change', command=self.do_button_press)
# Position button and canvas objects
button.grid(row=0, column=0)
canvas.grid(row=0, column=1)
root.mainloop() # Start the GUI event loop
def do_button_press(self):
"""
The window manager calls this function when the user
presses the graphical button
"""
for light in self.lights_list:
light.change()
if __name__ == '__main__':
# Create and execute one, multiple or random traffic lights
TrafficLightWindow()
# RandomLightsWindow()
# MultisizeLightsWindow()