-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGui.py
More file actions
432 lines (345 loc) · 14.9 KB
/
Gui.py
File metadata and controls
432 lines (345 loc) · 14.9 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
try:
import Tkinter as tk
import ScrolledText as tkst
except ImportError:
import tkinter as tk
import tkinter.scrolledtext as tkst
from tkinter import END, LEFT, RIGHT, TOP, BOTTOM, BOTH, Y, NE, NS, NSEW, W, E, YES
from tkinter import ttk
from tkinter.font import Font
from tkinter import VERTICAL, HORIZONTAL
import AnalysisCommands as ac
from TwitterCommands import getUserTweets
from TwitterCommands import getHashtagTweets
from datetime import date
import datetime
class tWindow(tk.Toplevel):
def __init__(self, master, frame_look={}, **look):
args = dict(relief=tk.SUNKEN, border=1)
args.update(frame_look)
tk.Toplevel.__init__(self, master, **args)
args = {'relief': tk.FLAT}
args.update(look)
self.textbox = tkst.ScrolledText(self, width = 20, height = 18)
self.textbox.pack(side=tk.TOP)
self.confirm = tk.Button(self, text='confirm', command=self.testingsystem)
self.confirm.pack(side=tk.TOP)
self.quitb = tk.Button(self, text='Quit', command=self.close_window)
self.quitb.pack()
def close_window(self):
self.destroy()
def testingsystem(self):
self.test = self.textbox.get('1.0', tk.END)
self.statuses = []
self.statuses.append(self.test)
#print(self.statuses)
self.newWindow = tk.Toplevel(self.master)
self.app = CellWindow(self.newWindow, intensities=ac.status_list_analysis(statuslist=self.statuses))
self.destroy()
class HashtagWindow(tk.Toplevel):
def __init__(self, master, frame_look={}, **look):
args = dict(relief=tk.SUNKEN, border=1)
args.update(frame_look)
tk.Toplevel.__init__(self, master, **args)
args = {'relief': tk.FLAT}
args.update(look)
self.topFrame = tk.Frame(self)
#Need a field for hashtag grabbing
self.label1 = tk.Label(self.topFrame, text='Hashtag(#)').pack(side=tk.LEFT)
self.hashentry = tk.Entry(self.topFrame)
self.hashentry.pack(side=tk.RIGHT)
self.topFrame.pack(side=tk.TOP)
self.fromFrame = tk.Frame(self)
#need a field for Start date grabbing
#month selection
self.label2 = tk.Label(self.fromFrame, text='From').pack(side=LEFT)
self.montha = tk.Entry(self.fromFrame,width=5)
self.montha.pack(side=tk.LEFT)
#day selection
self.daya = tk.Entry(self.fromFrame,width=5)
self.daya.pack(side=tk.LEFT)
#year selectinon
self.yeara = tk.Entry(self.fromFrame,width=5)
self.yeara.pack(side=tk.LEFT)
self.fromFrame.pack()
#need a field for end date grabbing
self.toFrame = tk.Frame(self)
#need a field for Start date grabbing
#month selection
self.label3 = tk.Label(self.toFrame, text='To').pack(side=LEFT)
self.monthb = tk.Entry(self.toFrame,width=5)
self.monthb.pack(side=tk.LEFT)
#day selection
self.confirm = tk.Button(self, text='confirm',anchor=tk.W, command=self.hashsearching)
self.confirm.pack(side=tk.TOP)
self.quitb = tk.Button(self, text='Quit', command=self.close_window, anchor=tk.W)
self.quitb.pack()
def close_window(self):
self.destroy()
def hashsearching(self):
#get the hashtag
try:
self.hashtag = self.hashentry.get()
except tk._tkinter.TclError:
print('ay no value')
self.destroy()
return 0
if self.hashtag == '':
print('no search')
self.destroy()
return 0
print(self.hashtag)
try:
self.month_a = self.montha.get()
self.day_a = self.daya.get()
self.year_a = self.yeara.get()
except tk._tkinter.TclError:
print('no search')
self.destroy()
return 0
try:
print(self.hashtag)
self.moa = int(self.month_a)
self.daa = int(self.day_a)
self.yea = int(self.year_a)
self.adate = datetime.datetime(self.yea,self.moa,self.daa,0,0)
self.statuses = getHashtagTweets(self.hashtag,self.adate)
self.temp = []
for tweet in self.statuses:
char_list = [tweet[j] for j in range(len(tweet)) if ord(tweet[j]) in range(65536)]
tweet=''
for j in char_list:
tweet=tweet+j
self.temp.append(tweet)
self.statuses = self.temp
self.newWindow = tk.Toplevel(self.master)
self.app = CellWindow(self.newWindow, intensities=ac.status_list_analysis(statuslist=self.statuses))
except ValueError:
print('Value Error occured only searching based on hash')
print('Searching based on hashtag')
self.destroy()
return 0
class introWindow(tk.Frame):
def __init__(self, master, frame_look={}, **look):
args = dict(relief=tk.SUNKEN, border=1)
args.update(frame_look)
tk.Frame.__init__(self, master, **args)
args = {'relief': tk.FLAT}
args.update(look)
self.button_1 = tk.Button(self, text='@',width=25, command=self.user_window)
self.button_1.pack(side = tk.TOP)
self.button_2 = tk.Button(self, text='#',command=self.hash_window, width=25)
self.button_2.pack(side = tk.TOP)
self.button_3 = tk.Button(self, text='T', command=self.t_window,width=25)
self.button_3.pack(side = tk.TOP)
self.button_4 = tk.Button(self, text='Quit', command=self.quit,width=25)
self.button_4.pack(side = tk.BOTTOM)
def t_window(self):
self.app = tWindow(self.master)
def hash_window(self):
self.app = HashtagWindow(self.master)
def user_window(self):
self.app = UserWindow(self.master)
class CellWindow:
SortDir = True
def __init__(self, master, intensities=''):
self.master = master
self.frame = tk.Frame( self.master, height=0, width=900 )
self.tree_frame = tk.Frame(self.frame, height=0, width=900)
self.tree_frame.pack_propagate(0)
self.tree_frame.pack()
self.dataCols = ('Tweet', 'Negative', 'Neutral', 'Positive', 'Compound')
self.tree = ttk.Treeview( self.frame, height = 14, columns= self.dataCols, selectmode="extended", show='headings')
for x in self.dataCols:
self.data = self.intensity_fixer( intensities=intensities)
self.tree.pack(fill=BOTH)
self._load_data()
self.quitButton = tk.Button(self.frame, text='Quit', width=25, command=self.close_windows)
self.quitButton.pack(side=LEFT)
self.exportButton = tk.Button(self.frame, text='Export', width=25, command=self.export_data)
self.exportButton.pack()
self.frame.pack()
def close_windows(self):
self.master.destroy()
def export_data(self):
file_obj = open('export.txt', 'w')
for item in self.data:
for x in item:
try:
file_obj.write(x + " ")
except UnicodeEncodeError:
continue
file_obj.write('\r\n')
def intensity_fixer(self, intensities=''):
#[songname, comp, neg, neu, pos, art, sample]
result =[]
for i in intensities:
temp = []
temp.append(i[0])
temp.append('{:05.2f}'.format(100 * i[1]))
temp.append('{:05.2f}'.format(100 * i[2]))
temp.append('{:05.2f}'.format(100 * i[3]))
temp.append('{:05.2f}'.format(100 * i[4]))
result.append(temp)
return result
def _load_data(self):
# configure column headings
for c in self.dataCols:
self.tree.heading(c, text=c.title(),
command=lambda c=c: self._column_sort(c, CellWindow.SortDir))
self.tree.column(c, width=Font().measure(c.title()))
# add data to the tree
for item in self.data:
self.tree.insert('', 'end', values=item)
def _column_sort(self, col, descending=False):
# grab values to sort as a list of tuples (column value, column id)
# e.g. [('Argentina', 'I001'), ('Australia', 'I002'), ('Brazil', 'I003')]
data = [(self.tree.set(child, col), child) for child in self.tree.get_children('')]
# reorder data
# tkinter looks after moving other items in
# the same row
data.sort(reverse=descending)
for indx, item in enumerate(data):
self.tree.move(item[1], '', indx) # item[1] = item Identifier
# and adjust column widths if necessary
for idx, val in enumerate(item):
iwidth = Font().measure(val)
if self.tree.column(self.dataCols[idx], 'width') < iwidth:
self.tree.column(self.dataCols[idx], width=iwidth)
# reverse sort direction for next sort operation
CellWindow.SortDir = not descending
class UserWindow(tk.Toplevel):
def __init__(self, master, frame_look={}, **look):
args = dict(relief=tk.SUNKEN, border=1)
args.update(frame_look)
tk.Toplevel.__init__(self, master, **args)
args = {'relief': tk.FLAT}
args.update(look)
self.topFrame = tk.Frame(self)
#Need a field for hashtag grabbing
self.label1 = tk.Label(self.topFrame, text='User').pack(side=tk.LEFT)
self.userentry = tk.Entry(self.topFrame)
self.userentry.pack(side=tk.RIGHT)
self.topFrame.pack(side=tk.TOP)
self.fromFrame = tk.Frame(self)
#need a field for Start date grabbing
#month selection
self.label2 = tk.Label(self.fromFrame, text='From').pack(side=LEFT)
self.montha = tk.Entry(self.fromFrame,width=5)
self.montha.pack(side=tk.LEFT)
#day selection
self.daya = tk.Entry(self.fromFrame,width=5)
self.daya.pack(side=tk.LEFT)
#year selectinon
self.yeara = tk.Entry(self.fromFrame,width=5)
self.yeara.pack(side=tk.LEFT)
self.fromFrame.pack()
#need a field for end date grabbing
self.toFrame = tk.Frame(self)
#need a field for Start date grabbing
#month selection
self.label3 = tk.Label(self.toFrame, text='To').pack(side=LEFT)
self.monthb = tk.Entry(self.toFrame,width=5)
self.monthb.pack(side=tk.LEFT)
#day selection
self.dayb = tk.Entry(self.toFrame,width=5)
self.dayb.pack(side=tk.LEFT)
#year selectinon
self.yearb = tk.Entry(self.toFrame,width=5)
self.yearb.pack(side=tk.RIGHT)
self.toFrame.pack()
self.confirm = tk.Button(self, text='confirm',anchor=tk.W, command=self.hashsearching)
self.confirm.pack(side=tk.TOP)
self.quitb = tk.Button(self, text='Quit', command=self.close_window, anchor=tk.W)
self.quitb.pack()
def close_window(self):
self.destroy()
def hashsearching(self):
#get the hashtag
try:
self.user = self.userentry.get()
except tk._tkinter.TclError:
print('ay no value')
self.destroy()
return 0
if self.user == '':
print('no search')
self.destroy()
return 0
print(self.user)
try:
self.month_a = self.montha.get()
self.day_a = self.daya.get()
self.year_a = self.yeara.get()
self.month_b = self.monthb.get()
self.day_b = self.dayb.get()
self.year_b = self.yearb.get()
except tk._tkinter.TclError:
print('no search')
self.destroy()
try:
print(self.user)
self.moa = int(self.month_a)
self.daa = int(self.day_a)
self.yea = int(self.year_a)
self.mob = int(self.month_b)
self.dab = int(self.day_b)
self.yeb = int(self.year_b)
self.adate = date(self.yea,self.moa,self.daa)
self.bdate = date(self.yeb,self.mob,self.dab)
if(self.adate > self.bdate):
#search from bdate to adate
print('Searchin B TO A')
self.bdate = datetime.datetime(self.yeb,self.mob,self.dab,0,0)
self.adate = datetime.datetime(self.yea,self.moa,self.daa,23,59)
self.statuses = getUserTweets(self.user,self.bdate,self.adate)
self.temp = []
for tweet in self.statuses:
char_list = [tweet[j] for j in range(len(tweet)) if ord(tweet[j]) in range(65536)]
tweet=''
for j in char_list:
tweet=tweet+j
self.temp.append(tweet)
self.statuses = self.temp
self.newWindow = tk.Toplevel(self.master)
self.app = CellWindow(self.newWindow, intensities=ac.status_list_analysis(statuslist=self.statuses))
else:
#search adate to bdate
print('Searchin A TO B')
self.bdate = datetime.datetime(self.yeb,self.mob,self.dab,23,59)
self.adate = datetime.datetime(self.yea,self.moa,self.daa,0,0)
self.statuses = getUserTweets(self.user,self.adate,self.bdate)
self.temp = []
for tweet in self.statuses:
char_list = [tweet[j] for j in range(len(tweet)) if ord(tweet[j]) in range(65536)]
tweet=''
for j in char_list:
tweet=tweet+j
self.temp.append(tweet)
self.statuses = self.temp
self.newWindow = tk.Toplevel(self.master)
self.app = CellWindow(self.newWindow, intensities=ac.status_list_analysis(statuslist=self.statuses))
except ValueError:
print('Value Error occured only searching based on hash')
print('Searching based on user')
self.start = datetime.datetime(2016, 1, 1, 0, 0)
self.end = datetime.datetime(2019, 12, 31, 23, 59)
self.statuses = getUserTweets(self.user,self.start,self.end)
self.temp = []
for tweet in self.statuses:
char_list = [tweet[j] for j in range(len(tweet)) if ord(tweet[j]) in range(65536)]
tweet=''
for j in char_list:
tweet=tweet+j
self.temp.append(tweet)
self.statuses = self.temp
self.newWindow = tk.Toplevel(self.master)
self.app = CellWindow(self.newWindow, intensities=ac.status_list_analysis(statuslist=self.statuses))
self.destroy()
if __name__ == '__main__':
win = tk.Tk()
win.title('Intro')
dentry = introWindow(win, font=('Helvetica', 40, tk.NORMAL), border=0)
dentry.pack()
#win.bind('<Return>', lambda e: print(dentry.get()))
win.mainloop()