-
Notifications
You must be signed in to change notification settings - Fork 3
/
interface.py
89 lines (82 loc) · 4.38 KB
/
interface.py
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
import tkinter as tk
class Interface(object):
def __init__(self, controller):
self.controller = controller
self.floors = controller.floors # number of floors
self.window = tk.Tk()
self.window.title("ElevatorSimulator")
self.displays = dict()
self.displays[0] = dict()
self.displays[0]["frame"] = tk.Frame(self.window, highlightbackground="black", highlightthickness=1)
self.displays[0]["frame"].grid(row=0, column=0)
tk.Label(self.displays[0]["frame"], text="Call Elevator").grid(row=0, column=0, columnspan=3)
tk.Label(self.displays[0]["frame"]).grid(row=1, column=0, columnspan=3) # space holder
self.displays[0]["calls"] = dict()
for i in range(1, self.floors + 1):
self.displays[0]["calls"][i] = dict()
self.displays[0]["calls"][i]["up"] = tk.Button(
self.displays[0]["frame"],
text="\u25B2",
relief="groove",
command=lambda f=i: self.controller.call_elevator(f, "up")
)
self.displays[0]["calls"][i]["down"] = tk.Button(
self.displays[0]["frame"],
text="\u25BC",
relief="groove",
command=lambda f=i: self.controller.call_elevator(f, "down")
)
tk.Label(self.displays[0]["frame"], text=i).grid(row=self.floors + 2 - i, column=0)
if i != self.floors:
self.displays[0]["calls"][i]["up"].grid(row=self.floors + 2 - i, column=1)
if i != 1:
self.displays[0]["calls"][i]["down"].grid(row=self.floors + 2 - i, column=2)
self.update_calls_display(False)
for i in range(1, len(self.controller.elevators) + 1):
display = dict()
display["frame"] = tk.Frame(self.window, highlightbackground="black", highlightthickness=1)
display["frame"].grid(row=0, column=i)
display["name"] = tk.Label(display["frame"], text=self.controller.elevators[i - 1].name)
display["name"].grid(row=0, column=0, columnspan=2)
display["floor"] = tk.Label(display["frame"])
display["floor"].grid(row=1, column=0)
display["dir"] = tk.Label(display["frame"])
display["dir"].grid(row=1, column=1)
display["go_to"] = dict()
for j in range(1, self.floors + 1):
display["go_to"][j] = tk.Button(display["frame"], text=j, width=3, relief="groove",
command=lambda e=i - 1, f=j: self.send_elevator(e, f))
display["go_to"][j].grid(row=self.floors + 2 - j, column=0, columnspan=2)
self.displays[i] = display
self.update_display(i, False)
def send_elevator(self, elevator, floor):
self.controller.elevators[elevator].go_to[floor] = True
def update_calls_display(self, continuous=True):
for i in range(1, self.floors + 1):
self.displays[0]["calls"][i]["up"]["bg"] = "gold" if self.controller.called[i]["up"] else "SystemButtonFace"
self.displays[0]["calls"][i]["down"]["bg"] = "gold" if self.controller.called[i][
"down"] else "SystemButtonFace"
self.window.update_idletasks()
if continuous:
self.window.after(500, self.update_calls_display)
def update_display(self, i, continuous=True):
elevator = self.controller.elevators[i - 1]
self.displays[i]["floor"]["text"] = elevator.current_floor
self.displays[i]["dir"]["text"] = "\u25B2" if elevator.going_up else "\u25BC"
for j in range(1, self.floors + 1):
# blue: open; green: current floor; gold: called; otherwise, default color
if elevator.current_floor == j:
self.displays[i]["go_to"][j]["bg"] = "blue" if elevator.door_open else "green"
elif elevator.go_to[j]:
self.displays[i]["go_to"][j]["bg"] = "gold"
else:
self.displays[i]["go_to"][j]["bg"] = "SystemButtonFace"
self.window.update_idletasks()
if continuous:
self.window.after(500, lambda d=i: self.update_display(d))
def update_displays(self):
self.update_calls_display()
for i in range(1, len(self.controller.elevators) + 1):
self.update_display(i)
def run(self):
self.window.after(0, self.update_displays)