-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.py
More file actions
280 lines (204 loc) · 10 KB
/
frontend.py
File metadata and controls
280 lines (204 loc) · 10 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from backend import SmartHome, SmartPlug, SmartWashingMachine
from tkinter import *
""" Global variables that include: backend manager, Tkinter window """
smartHome = SmartHome()
mainWin = Tk()
# This variables stores the labels for each device and the number of currently active devices (in index 0)
deviceLabels = []
# Constants
WINDOWDEFAULTS = [650, 400]
""" Start of the function library for the frontend of the coursework """
def setupHome():
""" This function is responsible for setting up the smart home with the default devices outined in task 4 """
for i in range(2):
smartHome.addDevice(SmartPlug())
for i in range(3):
smartHome.addDevice(SmartWashingMachine())
def turnoffAllDevices():
""" Turns off all the devices in the 'smartHome' object """
smartHome.turnoffAllDevices()
updateGUI()
def turnonAllDevices():
""" Turns on all the devices in the 'smartHome' object """
smartHome.turnonAllDevices()
updateGUI()
""" These functions are used to create the GUI elements"""
def createTopLine():
""" Creates the header information for the GUI"""
# Main title
titleLabel = Label(mainWin, text="Smart Home", font=("TkDefaultFont", 20, "bold"))
titleLabel.grid(row=0, column=0, columnspan=2)
# Global buttons for all devices
switchOffAllButton = Button(mainWin, text="Switch Off All", command=turnoffAllDevices)
switchOffAllButton.grid(row=1, column=0)
switchOnAllButton = Button(mainWin, text="Switch On All", command=turnonAllDevices)
switchOnAllButton.grid(row=2, column=0)
def createDevices():
""" This function creates both the labels and buttons for each device in the 'smartHome' object """
height = (len(smartHome.getDevices()) * 30) + 150
mainWin.geometry(f"{WINDOWDEFAULTS[0]}x{height}")
for i in range(len(smartHome.getDevices())):
# Anonymous function for toggling a given device
def toggleDevice(index=i):
""" Toggles a device at a given index in the 'smartHome' object """
device = smartHome.getDeviceat(index)
device.toggleSwitch()
updateGUI()
def customiseDevice(index=i):
""" Anonymous function to forward the index of the device to the customise window """
customiseWindow(index)
device = smartHome.devices[i]
deviceLabel = Label(mainWin, text=str(device))
deviceButton = Button(mainWin, text="Toggle this", command=toggleDevice)
customiseDeviceButton = Button(mainWin, text="Customise", command=customiseDevice)
deviceLabels.append(deviceLabel)
deviceLabel.grid(row=i + 3, column=0)
deviceButton.grid(row=i + 3, column=1)
customiseDeviceButton.grid(row=i + 3, column=2)
def createBottomLine():
""" This function creates the functionality for adding devices and removing devices from the smart home as well as displaying the number of active devices """
activeDevicesCount = Label(mainWin, text=f"Active Devices: {smartHome.getNumOnDevices()}")
activeDevicesCount.grid(row=len(smartHome.getDevices()) + 3, column=0)
deviceLabels.append(activeDevicesCount)
addDeviceButton = Button(mainWin, text="Add Device", command=addDeviceWindow)
addDeviceButton.grid(row=len(smartHome.getDevices()) + 4, column=0)
removeDeviceButton = Button(mainWin, text="Remove Device", command=removeDeviceWindow)
removeDeviceButton.grid(row=len(smartHome.getDevices()) + 4, column=1)
# The windows here are responsible for the challenge features
def addDeviceWindow():
""" This function is responsible for creating a window that allows the user to add a new device """
def addSmartPlug():
smartHome.addDevice(SmartPlug())
deleteGUI()
setupGUI()
updateGUI()
def addSmartWasher():
smartHome.addDevice(SmartWashingMachine())
deleteGUI()
setupGUI()
updateGUI()
addWin = Toplevel(mainWin)
addWin.title("Add Device")
addWin.resizable(False, False)
addLabel = Label(addWin, text="Add Device", font=("TkDefaultFont", 20, "bold"))
addLabel.grid(row=0, column=0, columnspan=2)
# Add Smart Plug Functionality
smartPlugLabel = Label(addWin, text="Smart Plug")
smartPlugLabel.grid(row=1, column=0)
smartPlugAddButton = Button(addWin, text="Add", command=addSmartPlug)
smartPlugAddButton.grid(row=1, column=1)
# Add Smart Washer Functionality
smartWasherLabel = Label(addWin, text="Smart Washing Machine")
smartWasherLabel.grid(row=2, column=0)
smartWasherAddButton = Button(addWin, text="Add", command=addSmartWasher)
smartWasherAddButton.grid(row=2, column=1)
def removeDeviceWindow():
""" This function is responsible for creating a window that allows the user to remove a device """
removeWin = Toplevel(mainWin)
removeWin.title("Remove Device")
removeWin.resizable(False, False)
removeLabel = Label(removeWin, text="Remove Device", font=("TkDefaultFont", 20, "bold"))
removeLabel.grid(row=0, column=0, columnspan=2)
for index in range(len(smartHome.getDevices())):
def removeDevice(index=index):
""" Removes devices from 'SmartHome' and removes its display label from the GUI """
removeWin.destroy()
smartHome.removeDeviceAt(index)
deviceLabels.remove(deviceLabels[index])
deleteGUI()
setupGUI()
updateGUI()
device = smartHome.getDeviceat(index)
deviceLabel = Label(removeWin, text=str(device))
deviceLabel.grid(row=index + 1, column=0)
removeButton = Button(removeWin, text="Remove", command=removeDevice)
removeButton.grid(row=index + 1, column=1)
def customiseWindow(index):
""" This function is responsible for creating a new window that will allow the user to customise the device """
# Device that is being customised
device = smartHome.getDeviceat(index)
# GUI setup and generation
customiseWin = Toplevel(mainWin)
customiseWin.title("Customise Device")
customiseWin.resizable(False, False)
customiseTitle = Label(customiseWin, text="Customise Device",font=("TkDefaultFont", 20, "bold"))
customiseTitle.grid(row=0, column=0, columnspan=2)
# Handle customisation differently for each device type
if type(device) == SmartPlug:
consumptionLabel = Label(customiseWin, text="Consumption (W):")
consumptionLabel.grid(row=1, column=0)
consumptionEntry = Entry(customiseWin)
consumptionEntry.insert(0, device.getConsumptionRate())
consumptionEntry.grid(row=1, column=1)
def confirmConsumption(d=device):
""" This anonymous function is responsible for confirming the new consumption rate for the device """
# Error handling for invalid data types
try:
newConsumptionRate = int(consumptionEntry.get())
# Error catching for out of bounds values
if not (0 <= newConsumptionRate <=150):
errorLabel.config(text=f"Value {newConsumptionRate} is out of bounds, please enter a number between 0 and 150")
else:
device.setConsumptionRate(consumptionEntry.get())
customiseWin.destroy()
updateGUI()
except:
newConsumptionRate = consumptionEntry.get()
errorLabel.config(text=f"Value {newConsumptionRate} is not Valid, please enter a number between 0 and 150")
confirmButton = Button(customiseWin, text="Confirm", command=confirmConsumption)
confirmButton.grid(row=1, column=2)
if type(device) == SmartWashingMachine:
washModeTitleLabel = Label(customiseWin, text="Wash Mode:")
washModeTitleLabel.grid(row=1, column=0)
for index in range(len(device.getWashModes())):
def confirmWashMode(washMode=index):
""" This anonymous function is responsible for confirming the new wash mode for the device """
device.setWashModeAt(washMode)
customiseWin.destroy()
updateGUI()
washModeLabel = Label(customiseWin, text=device.getWashModeAt(index))
washModeLabel.grid(row=index + 1, column=1)
washModeConfirmButton = Button(customiseWin, text="Confirm", command=confirmWashMode)
washModeConfirmButton.grid(row=index + 1, column=2)
errorLabel = Label(customiseWin, text="")
errorLabel.grid(row=100, column=0, columnspan=3)
updateGUI()
""" These function are used to manage the GUI and make sure it is up to date"""
def updateGUI():
""" This function updates each label by configuring the text to be the string function of each device """
# Check in place as to allow the GUI to be closed without an error occurring
if len(deviceLabels) > 0:
for index in range(len(smartHome.getDevices())):
device = smartHome.getDeviceat(index)
deviceLabel = deviceLabels[index]
deviceLabel.config(text=str(device))
deviceLabels[-1].config(text=f"Active Devices: {smartHome.getNumOnDevices()}")
def quit():
""" This function is used for the quit button at the top of the GUI """
deleteGUI()
mainWin.destroy()
""" Main functions that are outlined in the coursework specification """
def setupGUI():
""" The main setup function for the GUI """
# Window setup
mainWin.title("Smart Home")
mainWin.geometry(f"{WINDOWDEFAULTS[0]}x{WINDOWDEFAULTS[1]}")
mainWin.resizable(False, False)
# Custom exit button protocol
mainWin.protocol("WM_DELETE_WINDOW", quit)
createTopLine()
createDevices()
createBottomLine()
mainWin.mainloop()
def deleteGUI():
""" This function removes all widgets from the GUI and resets the deviceLabels list """
global deviceLabels
deviceLabels = []
for widget in mainWin.winfo_children():
widget.destroy()
def main():
""" Main function that is called to create both backend and frontend """
setupHome()
setupGUI()
# Run main function
main()