-
Notifications
You must be signed in to change notification settings - Fork 3
/
gui.py
116 lines (91 loc) · 2.9 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
from tkinter import *
from tkinter import ttk
from webapp import fetch_details
import threading
import random
import time
import os
global f, stop_event, pause_event
stop_event = threading.Event()
pause_event = threading.Event()
def calculate(*args):
try:
action.set("Start")
stop_event.clear()
params = int(id.get())
time,name = map(str,fetch_details(params).text.split(","))
title.set(name)
duration.set(time)
if time != "-1":
if not os.path.exists("./"+str(id.get())):
os.system("mkdir ./"+str(id.get()))
dir = "./"+str(id.get())
list = os.listdir(dir)
files = len(list)
file = "data_"+str(id.get())+"_"+str(files+1)+".txt"
os.system("touch "+dir+"/"+file)
except ValueError:
pass
# Daemon thread function to write the data to the file.
def background(t, stop_event):
curr = 0
try:
while curr < t and not stop_event.is_set():
if not pause_event.is_set():
dir = "./"+str(id.get())
list = os.listdir(dir)
files = len(list)
file = "data_"+str(id.get())+"_"+str(files)+".txt"
f = open(dir+"/"+file, 'a')
print("Writing file")
f.write(str(random.randint(1,100))+" ")
time.sleep(1)
curr += 1
duration.set(str(t-curr))
f.close()
except:
pass
def startIO():
try:
# Will prevent I/O error or seg fault during post request if it takes too long.
threading1 = threading.Thread(target=background, args = (int(duration.get()), stop_event))
# now threading1 runs regardless of user input
threading1.daemon = True
threading1.start()
except:
pass
def getdata(*args):
if action.get() == "Start":
action.set("Pause")
pause_event.clear()
startIO()
else:
action.set("Start")
pause_event.set()
def stop():
stop_event.set()
root = Tk()
root.title("Smart Learning System")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
id = StringVar()
title = StringVar()
duration = StringVar()
action = StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=id)
feet_entry.grid(column=2, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=title).grid(column=2, row=2, sticky=(W, E))
ttk.Label(mainframe, textvariable=duration).grid(column=2, row=3, sticky=(W, E))
ttk.Button(mainframe, text="Fetch Details", command=calculate).grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="Video ID :").grid(column=1, row=1, sticky=W)
ttk.Label(mainframe, text="Video Title :").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="Duration :").grid(column=1, row=3, sticky=E)
ttk.Button(mainframe, textvariable=action, command=getdata).grid(column=1, row=4, sticky=W)
action.set("Start")
ttk.Button(mainframe, text="Stop", command=stop).grid(column=3, row=4, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
feet_entry.focus()
#root.bind('<Return>', calculate)
root.mainloop()