-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
138 lines (103 loc) · 3.12 KB
/
gui.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
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
import time
import serial
import tkinter
import threading
import tkinter.ttk as ttk
import functions as core
def refreshAll():
loadTimes()
scrambleTxt["text"] = core.scramble()
def getAvg(n):
sum = 0
for i in range(0,n):
try:
sum+=float(cbb.get(i).replace("\n", ""))
except Exception:
return 0
return round(sum/n,4)
def loadTimes():
cbb.delete(0, tkinter.END)
for item in core.readTimes():
cbb.insert(0, item)
avg_three["text"] = "Avg. 3: "+str(getAvg(3))
avg_five["text"] = "Avg. 5: "+str(getAvg(5))
avg_twelve["text"] = "Avg. 12: "+str(getAvg(12))
avg_thirty["text"] = "Avg. 30: "+str(getAvg(30))
scrambleTxt["text"] = core.scramble()
def deleteItem():
try:
toDelete = cbb.get(cbb.curselection())
with open("times.data", "r") as f:
lines = f.readlines()
with open("times.data", "w") as f:
for line in lines:
if line != toDelete:
f.write(line)
loadTimes()
except:
pass
def timer():
s = serial.Serial(port.get())
tmpFrame.destroy()
timest = ""
timeend = ""
while 1:
ser = s.readline().decode("utf-8").replace("\n","")
if ser == "tmr_start\r":
timest = time.time()
txt.insert(tkinter.END, "Running!")
elif ser == "tmr_stop\r":
timeend = time.time()
if timest != "" and timeend != "":
timeCurr = str(round(timeend-timest,4))
txt.delete(0, tkinter.END)
txt.insert(0, timeCurr)
core.saveTime(timeCurr)
cbb.insert(0, timeCurr)
# loadTimes()
timeCurr = ""
timest = ""
timeend = ""
s.close()
def go():
t = threading.Thread(target=timer, daemon=True)
t.start()
#####################
### Layout
#####################
root = tkinter.Tk()
# root.geometry("500x500")
root.title("Cubetimer - GUI")
tmpFrame = tkinter.Frame()
tmpFrame.pack(side=tkinter.TOP)
frame = tkinter.Frame()
frame.pack()
btnFrame = tkinter.Frame()
btnFrame.pack(side=tkinter.TOP)
port = ttk.Combobox(tmpFrame)
port.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
ports = core.loadPorts()
port["values"] = ports
port.current(0)
startBtn = tkinter.Button(tmpFrame, text="Choose", command=go)
startBtn.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
txt = tkinter.Entry(frame, bd=5, insertwidth=1, font=30)
txt.pack()
scrambleTxt = tkinter.Label(frame)
scrambleTxt.pack()
avg_three = tkinter.Label(frame)
avg_three.pack()
avg_five = tkinter.Label(frame)
avg_five.pack()
avg_twelve = tkinter.Label(frame)
avg_twelve.pack()
avg_thirty = tkinter.Label(frame)
avg_thirty.pack()
refresh = tkinter.Button(btnFrame, text="Refresh", command=refreshAll)
refresh.pack(side=tkinter.LEFT)
dele = tkinter.Button(btnFrame, text="Delete", command=deleteItem)
dele.pack(side=tkinter.LEFT)
cbb = tkinter.Listbox(frame)
cbb.pack()
refreshAll()
root.mainloop()