-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkinter_window.py
607 lines (468 loc) · 26.2 KB
/
tkinter_window.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# importing inbuilt python libraries
import random
import time
from tkinter import *
# Importing my files
import client
from database import *
from network import *
class queue: # used to select a prompt, and make sure the prompt isn't the same twice in a row
def __init__(self, length):
self.nums = []
for i in range(0, length):
self.nums.append(i)
random.shuffle(self.nums) # shuffles nums
def refresh(self):
self.last = self.dequeue()
self.enqueue(self.last)
return self.last
def enqueue(self, item):
self.nums.insert(0, item) # puts it a front. Didn't use append since pop returns final element of list,
# so adding to queue from the front makes it easier
def dequeue(self):
return self.nums.pop()
class tkinter_windows:
def __init__(self):
self.db = database() # association- composition
self.login_success = False
def start_screen(self):
self.root = Tk()
self.root.geometry("300x250") # set window size
self.root.title("Handwriting tester") # set window title
Label(self.root, text="Please Select Your Choice", bg="white", width="20", height="2").pack() # pack puts it into screen
Label(self.root, text="").pack() # leaves a gap for aesthetic purposes
Button(self.root, text="Leaderboard", height="2", width="15",
command=self.__leaderboard).pack() # upon clicking, calls login function
Label(self.root, text="").pack()
Button(self.root, text="play", height="2", width="15",
command=self.__main_screen).pack() # upon clicking, calls register function
self.root.mainloop()
def __main_screen(self):
self.__main_screen = Toplevel(self.root) # create window
self.__main_screen.geometry("300x300") # set window size
self.__main_screen.title("Account selection") # set window title
Label(self.__main_screen, text="Please Select Your Choice", bg="white", width="20",
height="2").pack() # pack puts it into screen
Label(self.__main_screen, text="").pack() # leaves a gap for aesthetic purposes
Button(self.__main_screen, text="Login", height="2", width="15",
command=self.__login).pack() # upon clicking, calls login function
Label(self.__main_screen, text="").pack()
Button(self.__main_screen, text="Register", height="2", width="15",
command=self.__register).pack() # upon clicking, calls register function
Label(self.__main_screen, text="").pack()
Button(self.__main_screen, text="Continue", height="2", width="15", command=self.__continue).pack()
Label(self.__main_screen, text="").pack()
Button(self.__main_screen, text="Sign out", height="2", width="15", command=self.__logout).pack()
def __logout(self):
self.login_success = False
def __register(self):
self.register_screen = Toplevel(self.__main_screen) # like a stack
self.register_screen.title("Register")
self.register_screen.geometry("300x250")
# define how the text entered will be stored
self.username = StringVar()
self.password = StringVar()
Label(self.register_screen, text="Please enter your details below").pack()
Label(self.register_screen, text="").pack()
username_label = Label(self.register_screen, text="Username * ")
username_label.pack()
self.username_entry = Entry(self.register_screen, textvariable=self.username)
self.username_entry.pack()
password_label = Label(self.register_screen, text="Password * ")
password_label.pack()
self.password_entry = Entry(self.register_screen, textvariable=self.password, show='*')
self.password_entry.pack()
Label(self.register_screen, text="").pack()
Button(self.register_screen, text="Register", width=10, height=1, command=self.__register_user).pack()
def __register_user(self):
def register_failed(error_msg):
register_failed_screen = Toplevel(self.register_screen)
register_failed_screen.title("Error")
register_failed_screen.geometry("250x100")
Label(register_failed_screen, text=error_msg).pack()
Button(register_failed_screen, text="OK", command=register_failed_screen.destroy).pack()
def register_success():
register_success_screen = Toplevel(self.register_screen)
register_success_screen.title("Success")
register_success_screen.geometry("150x100")
Label(register_success_screen, text="You have been registered").pack()
Label(register_success_screen, text="You may now login").pack()
Button(register_success_screen, text="OK", command=register_success_screen.destroy).pack()
password = str(self.password.get())
username = str(self.username.get())
if 3 > len(username) or len(username) > 20: # username length
register_failed('that username length is invalid \n It must be >2 and <21')
elif ' ' in password or ' ' in username: # no whitespace
register_failed('Username must not include spaces')
elif not username.isalnum():
register_failed('Username must be alphanumeric')
elif len(password) < 8: # password length
register_failed('password must be longer than 8 characters')
elif self.db.user_in_db(username):
register_failed('That username is already in use')
else:
self.db.insert(username, password)
register_success()
def __login(self):
self.login_screen = Toplevel(self.__main_screen)
self.login_screen.title("Login")
self.login_screen.geometry("300x250")
Label(self.login_screen, text="Please enter your details below").pack()
Label(self.login_screen, text="").pack()
# define how the text entered will be stored
self.username_to_verify = StringVar()
self.password_to_verify = StringVar()
Label(self.login_screen, text="Username * ").pack()
self.username_login_entry = Entry(self.login_screen, textvariable=self.username_to_verify).pack()
Label(self.login_screen, text="").pack()
Label(self.login_screen, text="Password * ").pack()
self.password_login_entry = Entry(self.login_screen, textvariable=self.password_to_verify, show='*').pack()
def login_success():
self.login_success_screen = Toplevel(self.login_screen)
self.login_success_screen.title("Success")
self.login_success_screen.geometry("150x100")
Label(self.login_success_screen, text="Login Success").pack()
Button(self.login_success_screen, text="OK", command=self.login_success_screen.destroy).pack()
self.login_success = True
def login_failed(error_msg):
self.login_failed_screen = Toplevel(self.login_screen)
self.login_failed_screen.title("Error")
self.login_failed_screen.geometry("150x100")
Label(self.login_failed_screen, text=error_msg).pack()
Button(self.login_failed_screen, text="OK", command=self.login_failed_screen.destroy).pack()
self.login_success = False
def login_verify():
self.username = self.username_to_verify.get()
self.password = self.password_to_verify.get()
if self.db.user_password_match(self.username, self.password):
login_success()
else:
login_failed('invalid details')
Label(self.login_screen, text="").pack()
Button(self.login_screen, text="Confirm Login", width=10, height=1, command=login_verify).pack()
def __continue(self):
self.__continue_screen = Toplevel(self.__main_screen) # like a stack
self.__continue_screen.title("Welcome!")
self.__continue_screen.geometry("500x350")
Label(self.__continue_screen,
text="Welcome to the handwriting game - digit edition! \n In this game you will be tested on how neatly "
"you can write numbers.\n The computer will read your writing, \n and will give you an overall "
"score for how legible your writing was!\n").pack()
Label(self.__continue_screen, text="").pack()
Label(self.__continue_screen, text="").pack()
if self.login_success:
Label(self.__continue_screen, text=f"You are logged in as {self.username}").pack()
Button(self.__continue_screen, text='Play with a friend!', width=15, height=1,
command=self.__play_with_friend).pack()
elif not self.login_success:
Label(self.__continue_screen,
text="You are not logged in. Log in to be able to save your score, and play with a friend!.").pack()
Button(self.__continue_screen, text='Play solo!', width=15, height=1, command=self.__selection).pack()
def __play_with_friend(self):
self.__pw_friend = Toplevel(self.__continue_screen) # like a stack
self.__pw_friend.title("Selections")
self.__pw_friend.geometry("400x300")
self.n = Network()
Label(self.__pw_friend, text="Connected to the server.").pack()
self.n.send((self.username, 'init'))
t = 0
while True:
time.sleep(1) # to not overload the network, check every second.
t += 1
users = self.n.send(('play?', 'game')) # are they both loaded in?
if len(users) % 2 == 0: # waits until they have both loaded to break
break
elif t == 10:
self.__pw_friend.destroy()
self.n.send((self.username, 'unsend'))
return
def startw_friend():
self.n.send((int(self.num_images_selected.get()), 'num_games'))
num_games = self.n.send(('', 'num_games?'))
self.n.send((self.network_selected.get(), 'network'))
network = self.n.send(('', 'network?'))
Label(self.__pw_friend, text=f'There will be {num_games} rounds').pack()
begin_button = Button(self.__pw_friend, text='begin', width=15, height=2,
command=lambda: self.__beginw_friend(num_games, network, begin_button, player=1))
begin_button.pack()
if self.username == users[0]: # whoever loaded in first
Label(self.__pw_friend, text="You are player 1! You get to choose the number of games.").pack()
self.num_images_selected = StringVar()
self.network_selected = StringVar()
optionList = [str(x) for x in range(1, 21)]
self.num_images_selected.set(optionList[0])
Label(self.__pw_friend, text='Select the number of images you would like to go through').pack()
OptionMenu(self.__pw_friend, self.num_images_selected, *optionList).pack() # drop down menu
networkList = ['Smart network (harder)', 'Smarter network (easier)']
self.network_selected.set(networkList[0])
Label(self.__pw_friend, text='Select the difficulty of the recognition').pack()
OptionMenu(self.__pw_friend, self.network_selected, *networkList).pack()
Button(self.__pw_friend, text='confirm', width=15, height=2,
command=startw_friend).pack()
elif self.username == users[1]:
Label(self.__pw_friend, text=f"You are player 2! Wait until player 1 ({users[0]}) selects").pack()
t = 0
while True:
time.sleep(1) # to not overload the network, check every second.
reply = self.n.send(('selected?', 'selected?')) # are they both loaded in?
if reply == 'yes':
num_games = self.n.send(('', 'num_games?')) # server replies with number of games
network = self.n.send(('', 'network?')) # server replies with network choice
Label(self.__pw_friend, text=f'There will be {num_games} rounds').pack()
begin_button = Button(self.__pw_friend, text='Begin', width=15, height=2,
command=lambda: self.__beginw_friend(num_games, network, begin_button,
player=2))
begin_button.pack()
break
t += 1
if t == 10:
self.__pw_friend.destroy()
return
def __beginw_friend(self, num_games, network, b, player):
b.destroy()
self.n.send(('refresh', 'users'))
while True:
try:
client.server_predict(self.root, player, network, num_games)
break
except AttributeError: # if the server is not running
self.__warning_screen = Toplevel(self.__selection_screen)
self.__warning_screen.title("Error")
self.__warning_screen.geometry("300x100")
Label(self.__warning_screen, text='The server is not running. Please run the server to continue').pack()
return
self.accuracy = 0
self.num_correct = 0
self.num_attempts = 0
self.__begin_screen = Tk() # don't want this to be a top level of anything, since i want to keep this open
self.__begin_screen.title("Results")
self.__begin_screen.geometry("300x250")
self.db.insert_results(str(self.username)) # This creates a resultID for the user so that when rounds
# are being inserted into db, there is a valid resultID
self.result_button = Button(self.__begin_screen, text='Get results', width=15, height=2,
command=lambda: self.__get_results_friend(player))
self.result_button.pack()
self.__begin_screen.mainloop()
def __leaderboard(self):
self.__leaderboard_screen = Toplevel(self.root) # like a stack
self.__leaderboard_screen.title("Leaderboard")
self.__leaderboard_screen.geometry("400x350")
self.username_to_search = StringVar()
leaders = self.db.display_leaderboard()
row1 = 'Username, accuracy, number of attempts, number correct'
Label(self.__leaderboard_screen, text=row1).pack()
for row in leaders:
Label(self.__leaderboard_screen, text=f'{row[0]} | {row[1]} | {row[2]} | {row[3]} ').pack()
Entry(self.__leaderboard_screen, textvariable=self.username_to_search).pack()
Button(self.__leaderboard_screen, text='search username', width=15, height=2,
command=self.__find_username).pack()
def __find_username(self):
user = str(self.username_to_search.get()) # gets entered text
scores = self.db.get_user_scores(user) # returns sql statement that grabs all scores by that username
row1 = 'Round, Correct, Prompt, Seen, Certainty(%)'
score_screen = Toplevel(self.__leaderboard_screen)
score_screen.title("User scores")
score_screen.geometry("400x200")
Label(score_screen, text=row1).pack()
for i, row in enumerate(scores):
if row != 'nothing entered':
row = str(row)[:-1]
row = row[1:]
Label(score_screen, text=f'{i}| {row}').pack()
def __selection(self):
self.__selection_screen = Toplevel(self.__continue_screen) # like a stack
self.__selection_screen.title("Selections")
self.__selection_screen.geometry("400x300")
self.num_images_selected = StringVar()
self.network_selected = StringVar()
optionList = [x for x in range(1, 21)]
self.num_images_selected.set(optionList[0])
Label(self.__selection_screen, text='Select the number of images you would like to go through').pack()
OptionMenu(self.__selection_screen, self.num_images_selected, *optionList).pack() # drop down menu
networkList = ['Smart network (harder)', 'Smarter network (easier)']
self.network_selected.set(networkList[0])
Label(self.__selection_screen, text='Select the difficulty of the recognition').pack()
OptionMenu(self.__selection_screen, self.network_selected, *networkList).pack() # drop down menu
begin_button = Button(self.__selection_screen, text='begin', width=15, height=2,
command=lambda: self.__begin(begin_button))
begin_button.pack()
def __begin(self, b):
b.destroy()
num_images = int(self.num_images_selected.get())
network = str(self.network_selected.get())
while True:
try:
client.server_predict(self.root, 1, network, num_images)
break
except AttributeError: # if the server is not running, can use local
self.__warning_screen = Toplevel(self.__selection_screen)
self.__warning_screen.title("Error")
self.__warning_screen.geometry("300x100")
Label(self.__warning_screen, text='The server is not running. Please run the server to continue').pack()
return
self.accuracy = 0
self.num_correct = 0
self.num_attempts = 0
self.__begin_screen = Tk() # don't want this to be a top level of anything, since i want to keep this open
self.__begin_screen.title("Results")
self.__begin_screen.geometry("300x250")
if self.login_success:
self.db.insert_results(str(self.username)) # This creates a resultID for the user so that when rounds
# are being inserted into db, there is a valid resultID
self.result_button = Button(self.__begin_screen, text='Get results', width=15, height=2,
command=self.__get_results)
self.result_button.pack()
self.__begin_screen.mainloop()
def __get_results(self):
self.result_button.destroy() # destroy button so that user cannot press multiple times
f = open('user_score1.txt', 'r')
for row in f:
values = []
new_row = row[:-1] # remove \n by list slicing
for num in new_row.split():
values.append(num)
correct = False
attempt_num = int(values[0]) # first digit
prompt = int(values[1]) # next digit, after whitespace
try:
guess = int(values[2]) # next digit, after whitespace
certainty = int(values[3]) # rest of the string, after whitespace
except ValueError: # if None has been written, ValueError for int('N'). None written when user doesnt draw
guess = None
certainty = 0
if guess is None:
Label(self.__begin_screen,
text=f"Image number{attempt_num}\n You should have drawn a {prompt}.\n You drew nothing.").pack()
else:
Label(self.__begin_screen,
text=f"Image number{attempt_num}\n You should have drawn a {prompt}.\n I think that's a {guess} I'm {certainty}% certain").pack()
if guess == prompt and certainty > 60:
correct = True
self.num_correct += 1
Label(self.__begin_screen,text=" Great drawing!").pack()
else:
Label(self.__begin_screen,text=f" That is not a neat {prompt}, so I have falsely recognised it.").pack()
self.num_attempts += 1
if self.login_success:
self.db.insert_round(str(self.username), correct, prompt, guess, certainty)
try:
if self.login_success:
self.db.c.execute('select UserID from Users Where username = ?', (self.username,))
userID = self.db.c.fetchone()[0]
self.db.c.execute('''SELECT MAX(ResultID)
FROM Users, Results
Where Results.UserID = Users.UserID
AND Users.UserID = ?
''', (userID,))
newest_resultID = self.db.c.fetchone()[0]
# gets average accuracy of user rounds
self.db.c.execute('''SELECT AVG(Rounds.certainty)
FROM Rounds, Users, Results
Where (Users.UserID = Rounds.UserID) AND (Results.UserID = Rounds.UserID)
AND Users.UserID = ?
AND Rounds.ResultID = ?
AND Rounds.Correct = ?
''', (userID, newest_resultID, 'Yes'))
self.accuracy = self.db.c.fetchone()[0]
if self.accuracy is None:
self.accuracy = 0
self.db.update_results(self.username, self.accuracy, self.num_attempts, self.num_correct)
except sqlite3.ProgrammingError: # if database is closed i.e same user tried to play twice in one instance
pass
def __get_results_friend(self, player): # split page into 2, top for u and bot for friend
self.result_button.destroy() # destroy button so that user cannot press multiple times
f = open('user_score1.txt', 'r')
for row in f:
values = []
new_row = row[:-1] # remove \n by list slicing
for num in new_row.split():
values.append(num)
correct = False
attempt_num = int(values[0]) # first digit
prompt = int(values[1]) # next digit, after whitespace
try:
guess = int(values[2]) # next digit, after whitespace
certainty = int(values[3]) # rest of the string, after whitespace
except ValueError: # if None has been written, ValueError for int('N'). None written when user doesnt draw
guess = None
certainty = 0
Label(self.__begin_screen,
text=f"Image number{attempt_num}\n You should have drawn a {prompt}.\n I think that's a {guess} I'm {certainty}% certain").pack()
if guess == prompt and certainty > 60:
correct = True
self.num_correct += 1
Label(text=" Great drawing!").pack()
else:
Label(text=" You've not drawn that well enough for me to recognise it.").pack()
self.num_attempts += 1
self.db.insert_round(str(self.username), correct, prompt, guess, certainty)
Label(text='').pack()
while True:
time.sleep(.5) # to not overload the network, check every second.
if self.n.send(('ready?', 'ready?')) == 'yes':
break
if player == 1:
Label(self.__begin_screen, text="Player 2(your friend)").pack()
file = self.n.send(('2', 'predictions?'))
for row in file:
values = []
for num in row.split():
values.append(num)
attempt_num = int(values[0]) # first digit
prompt = int(values[1]) # next digit, after whitespace
try:
guess = int(values[2]) # next digit, after whitespace
certainty = int(values[3]) # rest of the string, after whitespace
except ValueError: # if None has been written, ValueError for int('N'). None written when user doesnt draw
guess = None
certainty = 0
Label(self.__begin_screen,
text=f"Image number {attempt_num}\n You should have drawn a {prompt}.\n I think that's a {guess} I'm {certainty}% certain").pack()
if guess == prompt and certainty > 60:
Label(text=" Great drawing!").pack()
else:
Label(text=" Not a good enough drawing for me to recognise.").pack()
elif player == 2:
Label(self.__begin_screen, text="Player 1(your friend)").pack()
file = self.n.send(('1', 'predictions?'))
for row in file:
values = []
for num in row.split():
values.append(num)
attempt_num = int(values[0]) # first digit
prompt = int(values[1]) # next digit, after whitespace
try:
guess = int(values[2]) # next digit, after whitespace
certainty = int(values[3]) # rest of the string, after whitespace
except ValueError: # if None has been written, ValueError for int('N'). None written when user doesnt draw
guess = None
certainty = 0
Label(self.__begin_screen,
text=f"Image number{attempt_num}\n You should have drawn a {prompt}.\n I think that's a {guess} I'm {certainty}% certain").pack()
if guess == prompt and certainty > 60:
Label(text=" Great drawing!").pack()
else:
Label(text="Not a good enough drawing for me to recognise.").pack()
self.db.c.execute('select UserID from Users Where username = ?', (self.username,))
userID = self.db.c.fetchone()[0]
self.db.c.execute('''SELECT MAX(ResultID)
FROM Users, Results
Where Results.UserID = Users.UserID
AND Users.UserID = ?
''', (userID,))
newest_resultID = self.db.c.fetchone()[0]
# gets average certainty of user rounds
self.db.c.execute('''SELECT AVG(Rounds.certainty)
FROM Rounds, Users, Results
Where (Users.UserID = Rounds.UserID) AND (Results.UserID = Rounds.UserID)
AND Users.UserID = ?
AND Rounds.ResultID = ?
AND Rounds.Correct = ?
''', (userID, newest_resultID, 'Yes')) # only get correct rounds since
self.accuracy = self.db.c.fetchone()[0]
if self.accuracy is None:
self.accuracy = 0
# self.n.send(('update results', 'update_results'))
self.db.update_results(self.username, self.accuracy, self.num_attempts, self.num_correct)
win = tkinter_windows()
win.start_screen()