This repository was archived by the owner on Jul 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask5_2_gui.py
More file actions
189 lines (148 loc) · 4.12 KB
/
Copy pathtask5_2_gui.py
File metadata and controls
189 lines (148 loc) · 4.12 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import tkinter as tk
from gpiozero import LED, PWMLED
# These are the GPIO pins I used for each light
# The living room light uses PWMLED because it needs brightness control
living_room_led = PWMLED(17)
bathroom_led = LED(27)
closet_led = LED(22)
# These variables help the program remember if each light is on or off
living_room_on = False
bathroom_on = False
closet_on = False
def toggle_living_room():
global living_room_on
living_room_on = not living_room_on
if living_room_on:
# Full brightness when the button is turned on
living_room_led.value = 1
living_room_button.config(text="Living Room ON")
living_slider.set(100)
status_label.config(text="Living room brightness: 100%")
else:
# Zero brightness means the LED is off
living_room_led.value = 0
living_room_button.config(text="Living Room OFF")
living_slider.set(0)
status_label.config(text="Living room brightness: 0%")
def toggle_bathroom():
global bathroom_on
bathroom_on = not bathroom_on
if bathroom_on:
bathroom_led.on()
bathroom_button.config(text="Bathroom ON")
else:
bathroom_led.off()
bathroom_button.config(text="Bathroom OFF")
def toggle_closet():
global closet_on
closet_on = not closet_on
if closet_on:
closet_led.on()
closet_button.config(text="Closet ON")
else:
closet_led.off()
closet_button.config(text="Closet OFF")
def change_living_brightness(value):
global living_room_on
# The slider gives a number from 0 to 100
# PWMLED needs a value between 0 and 1, so I divide by 100
brightness = int(value)
living_room_led.value = brightness / 100
if brightness == 0:
living_room_on = False
living_room_button.config(text="Living Room OFF")
else:
living_room_on = True
living_room_button.config(text="Living Room ON")
status_label.config(text=f"Living room brightness: {brightness}%")
def turn_all_off():
global living_room_on, bathroom_on, closet_on
# Turn all lights off together
living_room_led.value = 0
bathroom_led.off()
closet_led.off()
living_room_on = False
bathroom_on = False
closet_on = False
living_room_button.config(text="Living Room OFF")
bathroom_button.config(text="Bathroom OFF")
closet_button.config(text="Closet OFF")
living_slider.set(0)
status_label.config(text="Living room brightness: 0%")
def exit_program():
# Turn everything off before closing the program
living_room_led.value = 0
bathroom_led.off()
closet_led.off()
window.destroy()
# Main window for the GUI
window = tk.Tk()
window.title("Task 5.2C Light Intensity GUI")
window.geometry("420x430")
heading_label = tk.Label(
window,
text="Linda's Home Light Control",
font=("Arial", 16)
)
heading_label.pack(pady=10)
instruction_label = tk.Label(
window,
text="Use the buttons and slider to control the lights"
)
instruction_label.pack(pady=5)
living_room_button = tk.Button(
window,
text="Living Room OFF",
width=22,
command=toggle_living_room
)
living_room_button.pack(pady=5)
slider_label = tk.Label(
window,
text="Living Room Light Intensity"
)
slider_label.pack(pady=5)
living_slider = tk.Scale(
window,
from_=0,
to=100,
orient=tk.HORIZONTAL,
length=260,
command=change_living_brightness
)
living_slider.pack(pady=5)
status_label = tk.Label(
window,
text="Living room brightness: 0%"
)
status_label.pack(pady=5)
bathroom_button = tk.Button(
window,
text="Bathroom OFF",
width=22,
command=toggle_bathroom
)
bathroom_button.pack(pady=5)
closet_button = tk.Button(
window,
text="Closet OFF",
width=22,
command=toggle_closet
)
closet_button.pack(pady=5)
all_off_button = tk.Button(
window,
text="Turn All Lights OFF",
width=22,
command=turn_all_off
)
all_off_button.pack(pady=10)
exit_button = tk.Button(
window,
text="Exit",
width=22,
command=exit_program
)
exit_button.pack(pady=5)
window.protocol("WM_DELETE_WINDOW", exit_program)
window.mainloop()