-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockScreen.py
More file actions
131 lines (102 loc) · 4.57 KB
/
lockScreen.py
File metadata and controls
131 lines (102 loc) · 4.57 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
from tkinter import *
from windowSetting import setCenter
class LockInterface():
windowHeight = 200
windowWidth = 300
def __init__(self, title = "Door Lock", code = [1,2,3,4], locked = True):
self.title = title
self.numRows = len(code)
self.code = code
self.currCode = [0] * self.numRows
self.locked = locked
def openWindow(self):
self.window = Toplevel()
self.window.title(self.title)
setCenter(self.window, self.windowWidth, self.windowHeight)
self.canvas = Canvas(self.window, bg="white", highlightthickness=0)
self.canvas.pack(fill="both", expand=True)
self.drawLock()
def isLocked(self):
return self.locked
def getWindow(self):
return self.window
def drawLock(self):
lockLeft = self.windowWidth // 8
lockRight = (self.windowWidth * 7) // 8
lockY = self.windowHeight - 10
lockH = self.windowHeight // 2
self.canvas.delete("all")
self.canvas.create_rectangle(
lockLeft, lockY,
lockRight, lockY - lockH,
fill="#c3a55b", outline="black")
pinwidth = (lockRight - lockLeft) // (self.numRows + 1)
gap = ((lockRight - lockLeft) - (pinwidth * self.numRows)) // (self.numRows + 1)
self.canvas.create_line(lockRight - gap // 3, lockY - lockH // 2,
lockLeft + gap // 3, lockY - lockH // 2 + 1,
fill="white", width=10)
latchW = 15
latchBottom = lockY - lockH
connectorLeftX = lockLeft + latchW
connectorRightX = lockRight - latchW
diff = lockH // 5 if not self.locked else 0
self.canvas.create_arc(connectorLeftX, latchBottom + lockH // 3 - diff,
connectorRightX, latchBottom - lockH // 3 - diff,
start= 0, extent=180, style="arc", width=latchW,
outline="silver", tags=("latch"))
if not self.locked:
self.canvas.create_line(connectorLeftX, latchBottom,
connectorLeftX, latchBottom - diff,
fill="silver", width=latchW, tags=("latch"))
self.canvas.tag_bind(f"latch", "<Button-1>", lambda e: self.lock())
for i in range(self.numRows):
numLeft = lockLeft + gap * (i + 1) + pinwidth * i
numRight = numLeft + pinwidth
numY = lockY - 5
numH = lockH - 5
self.canvas.create_rectangle(
numLeft, numY,
numRight, numY - numH,
fill="gray", outline="black")
self.canvas.create_text(
(numLeft + numRight) // 2, (2 * numY - 1.5 *numH) // 2,
text=f" {(self.currCode[i] + 9) % 10} ",
anchor="center",
tags=(f"prevNum{i}")
)
self.canvas.create_text(
(numLeft + numRight) // 2, ((2 * numY - 0.5 *numH)) // 2,
text=f" {(self.currCode[i] + 1) % 10} ",
anchor="center",
tags=(f"nextNum{i}")
)
self.canvas.create_text(
(numLeft + numRight) // 2, ((2 * numY - numH)) // 2,
text=f" {self.currCode[i]} ",
anchor="center",
tags=(f"number{i}")
)
self.canvas.tag_bind(f"number{i}", "<Button-1>", lambda e, i=i: self.increaseNum(i))
self.canvas.tag_bind(f"number{i}", "<Button-3>", lambda e, i=i: self.decreaseNum(i))
self.canvas.tag_bind(f"prevNum{i}", "<Button-1>", lambda e, i=i: self.decreaseNum(i))
self.canvas.tag_bind(f"nextNum{i}", "<Button-1>", lambda e, i=i: self.increaseNum(i))
def increaseNum(self, i):
self.currCode[i] = (self.currCode[i] + 1) % 10
self.drawLock()
def decreaseNum(self, i):
self.currCode[i] = (self.currCode[i] + 9) % 10
self.drawLock()
def lock(self):
if not self.locked:
self.locked = True
self.drawLock()
return
for i in range(self.numRows):
if (self.currCode[i] != self.code[i]):
return
self.locked = False
self.drawLock()
if __name__ == "__main__":
lock = LockInterface()
lock.openWindow()
lock.getWindow().mainloop()