-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_Project.py
531 lines (355 loc) · 15.6 KB
/
_Project.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
"""
Project Name : ChatBot
Developers : AKHIL VARMA & VINAY
Description :
A simple chatbot which reads the text files provided to it and gives answers accordingly.
If it does not answer the question u have asked than u can feed any answer for your question.
The answer u have feeded will be stored in the file automatically for later use.
Problems :
-> It can not answer any question which is not present in the file which is fed into it.
-> It would give u an error if u will write the last line of the file as question.
Use your own files :
U can use your own files but following things should be done carefully :
-> The file should have ".txt" extension.
-> Question should be entered on one line without any spaces.
-> Answer should be on the very next line of the question. Write the answer in the way u want to display it.
"""
"""
importing all required
modules
"""
import time
import tkinter
from tkinter import *
#------------------------------------------------------------------------------------------
""" colors for later use"""
c1 = '#263238'
c2 = '#faa21f'
c3 = '#1e282d'
c6 = '#577e75'
c4 = '#faa21f'
c5 = '#577e75'
c7 = '#1e282d'
c8 = '#faa21f'
# Alternative Chat Colours
'''
c4 = '#4790f9'
c5 = '#00a3cf'
c7 = '#85c0f6'
c8 = '#83e7f2'
'''
#-------------------------------------------------------------------------------------------
"""
getting data from entry of
'Enter Name' page
"""
def info () :
global myname
myname = entry_user.get('1.0' , 'end-1c')
global chatbot
chatbot = entry_chat.get('1.0' , 'end-1c')
if myname == "" or chatbot == "":
Label(frame_info , text = "Fill both fields to proceed." , bg = "red" , fg = "white" , font = 'Verdana 11 bold').place( x = 182 , y = 96)
return
entry_user.delete('1.0' , END)
entry_chat.delete('1.0' , END)
frame_info.pack_forget ()
frame_topic.pack ()
#------------------------------------------------------------------------------------------
"""
opening files after selection of
topic in topic selestion
page
"""
def topic_1():
global no_topic
no_topic = 1
global top
top = 'd1_technology.txt'
global a
a = open( top , 'r')
global doc
doc = a.readlines()
frame_topic.pack_forget ()
frame_chat.pack()
topic = 'Technology'
label_topic.config(text = topic)
refresh_screen()
def topic_4():
global no_topic
no_topic = 4
global top
top = 'd4_physics.txt'
a = open(top , 'r')
global doc
doc = a.readlines()
frame_topic.pack_forget ()
frame_chat.pack()
topic = ' Units of physical Quantities '
label_topic.config(text = topic)
refresh_screen()
def topic_5():
global no_topic
no_topic = 5
global top
top = 'd5_computer.txt'
a = open( top , 'r')
global doc
doc = a.readlines()
frame_topic.pack_forget ()
frame_chat.pack()
topic = ' Input Output Devices '
label_topic.config(text = topic)
refresh_screen()
#----------------------------------------------------------------------------------------------------------
"""
functions for writing in files
for writing in files in chat screen
"""
def write_ans () :
enter1 = entry_feed.get('1.0' , END)
b.write(enter1)
b.close()
window.destroy()
"""
Reopening of files after
changes are saved
"""
if no_topic == 1 :
topic_1()
elif no_topic == 4 :
topic_4()
elif no_topic == 5 :
topic_5()
def feed_answer () :
"""
a seperate window for writing answer
on file
"""
global window
window=Tk()
frame_root = Frame(window , bg = c1)
frame_root.pack()
label = Label (frame_root , text = 'Enter the answer of Question here ...' , bg = c1 , fg = 'white' )
label.pack()
global entry_feed
entry_feed = Text (frame_root , height = 3 , width = 30 , fg = 'white' , bg = c2)
entry_feed.bind ('<Return>' , write_ans)
entry_feed.pack()
button = Button (frame_root , text = 'Add answer' , command = write_ans , bg = c3 , fg = 'white' )
button.pack()
def write_file () :
"""
opening file for appending in
exsisting files
"""
global b
b = open ( top , 'a')
b.write(chat_raw)
b.write('\n')
button_write.place_forget()
feed_answer ()
#----------------------------------------------------------------------------------------------
def refresh_screen () :
for widget in frame_chats.winfo_children():
widget.destroy()
button_write.place_forget()
label_space = Label (frame_chats , bg = c1 , text = '')
label_space.pack()
#------------------------------------------------------------------------------------------
def submit() :
"""
function for producing response of
request of user
"""
button_write.place_forget ()
global chat_raw
chat_raw = entry.get('1.0' , 'end-1c')
entry.delete('1.0' , END)
chat = chat_raw.lower()
chat = chat.replace(' ','')
global label_request
label_request = Label(frame_chats ,text=chat_raw , bg = c4 , fg= c7 , justify = LEFT , wraplength = 300, font = 'Verdana 10 bold')
label_request.pack(anchor = 'w')
global answer
if chat == 'groupmembers' or chat == 'group' or chat == 'developers' or chat == 'groupmember':
answer = "AKHIL VARMA \nVINAY"
elif chat == "what'smyname?" or chat == "whatsmyname?" or chat == "whatismyname?" or chat == "whatsmyname" or chat == 'myname?' or chat =='myname' :
answer = myname
elif chat == "what'syourname?" or chat == "whatisyourname?" or chat == "whatsyourname?" or chat == "whatsyourname" or chat == 'yourname?' or chat =='yourname' :
answer = chatbot
elif chat == 'bye' or chat == 'goodbye' or chat == 'exit' or chat == 'close' or chat == 'end' :
answer = 'Bye'
else:
i = 0
j = 0
for lines in doc:
stats = lines [:-1]
stats = stats.lower()
stat = stats.replace(' ','')
i += 1
if stat == chat :
answer = doc[i]
break
else:
j += 1
if i == j :
answer = "I don't understand.........please teach me ! "
button_write.place(x=430,y=3)
get_response()
def get_response() :
global label_response
label_response = Label(frame_chats ,text= answer ,bg= c5 , fg = c8 , justify = LEFT , wraplength = 300, font = 'Verdana 10 bold')
label_response.pack(anchor = 'e')
if answer == 'Bye':
root.destroy()
#------------------------------------------------------------------------------------------------------------------
"""
moving from one page to another
by help of button
"""
def welcome_to_info ():
frame_welcome.pack_forget ()
frame_info.pack ()
def info_to_topic ():
frame_info.pack_forget ()
frame_topic.pack ()
def topic_to_chat ():
frame_topic.pack_forget ()
frame_chat.pack()
def chat_to_topic ():
frame_chat.pack_forget ()
frame_topic.pack ()
def topic_to_info ():
frame_topic.pack_forget ()
frame_info.pack ()
def info_to_welcome ():
frame_info.pack_forget ()
frame_welcome.pack ()
#-----------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------
"""
calling constructor to make window
"""
root = Tk ()
#----------------------------------------------------------------------------------------------------
""" images used in window """
back = PhotoImage(file = 'arrow_behind.png')
front = PhotoImage(file = 'arrow_ahead.png')
exitt = PhotoImage(file = 'exit.png')
screen_1 = PhotoImage(file = 'image_5.png')
submit_img = PhotoImage(file = 'image_8.png')
#---------------------------------------------------------------------------------------------------------------------
""" WELCOME FRAME """
""" first frame containing time date and welcome messages """
frame_welcome = Frame (root , bg = c1 , height = '670' , width = '550')
frame_welcome.pack_propagate(0)
frame_welcome.pack()
welcome = Label (frame_welcome , text = 'Welcome' , font = "Vardana 40 bold" , bg = c1 , fg = "white")
welcome.place(x = 160 , y = 200)
welcome_chatbot = Label (frame_welcome , text = 'I am Chatbot ! ' , font = "Helvetica 15 bold italic" , bg = c1 , fg = c6)
welcome_chatbot.place(x = 200 , y = 270)
pic_1 = Label (frame_welcome , image = screen_1)
pic_1.place(x = -2 , y = 357 )
button_front = Button (frame_welcome , image = front , relief = "flat" , bg = c1 , bd = "3px solid black" , command = welcome_to_info ).place(x=470 , y=10)
#__________________________________________________________________
""" time option """
def clock () :
current = time.strftime("%H:%M:%S")
label_time = Label (frame_welcome , bd = 5 , text = current , height = 1 , width = 8 , font = 'Ariel 11 bold' , fg = "white" , relief = 'groove' , bg = c3)
label_time.place(x= 120 , y = 63)
label_time.after( 1000 , clock )
button_time = Button (frame_welcome , text = 'Time' , height = 1 , font = 'Vardana 10 bold' , width = 8 , bg = c2 , fg = c1 , command = clock)
button_time.place(x=30 , y = 63)
#_____________________________________________________________________________
""" date option """
def date () :
try:
date = time.strftime("%d %B , 20%y")
label_date = Label (frame_welcome , bd = 5 , relief = 'groove' , text = date , bg = c3 , fg = "white" , height = 1 , font = 'Ariel 11 bold')
label_date.place(x= 400 , y = 63)
label_date.after(86400000 , date)
except AttributeError:
print('')
button_date = Button (frame_welcome , text = 'Date' ,height = 1 , font = 'Vardana 10 bold' , width = 8 , bg = c2 , fg= c1 , command = date)
button_date.place(x = 310 , y = 63)
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
""" INFO FRAME """
""" frame of entering names """
frame_info = Frame (root , bg = c1 , height = '670' , width = '550')
frame_info.pack_propagate(0)
spacer1 = Label(frame_info , bg = c1)
spacer1.pack()
spacer2 = Label(frame_info , bg = c1)
spacer2.pack()
label_sub = Label (frame_info ,text = "Enter Information" , bg = c1 , fg = "white" , font = 'Verdana 30 italic')
label_sub.pack()
user_name = Label (frame_info , text = 'Enter your name : ' , bg = c1 , fg = c2 , font = 'Ariel 15')
user_name.place(x = 80,y=130)
entry_user = Text (frame_info , bg = c6, fg = "white" , height ='1' , width ='40' , font = 'Ariel 15')
entry_user.focus()
entry_user.place(x = 80 , y = 170)
chatbot_name = Label (frame_info , text = 'Give Chatbot a Name : ' , bg = c1 , fg = c2 , font = 'Ariel 15')
chatbot_name.place(x = 80 , y = 220)
entry_chat = Text (frame_info , bg = c6, fg = "white" , height ='1' , width ='40' , font = 'Ariel 15')
entry_chat.place(x = 80 , y = 260)
button_1 = Button (frame_info , text ='submit' , font = 'Vardana 10 bold' , bg = c2 , fg = c1 , command = info )
button_1.place(x = 470 , y = 330)
button_back = Button (frame_info , image = back , relief = "flat" , bg = c1 , command = info_to_welcome).place(x=10 , y = 10)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
""" TOPIC FRAME """
"""" frame for topic selection """
frame_topic = Frame (root , bg = c1 , height = '670' , width = '550')
frame_topic.pack_propagate(0)
spacer3 = Label(frame_topic , bg = c1)
spacer3.pack()
spacer4 = Label(frame_topic , bg = c1)
spacer4.pack()
select_label = Label (frame_topic , text = "Select Topic" , bg = c1 , fg = "white" , font = 'Ariel 30 italic')
select_label.pack()
spacer5 = Label(frame_topic , bg = c1)
spacer5.pack()
option_1 = Label (frame_topic , text = '1- Technology' , font = 'Verdana 15 italic' , bg = c1 , fg= c2)
option_1.place(x = 30 , y = 120)
button_opt_1 = Button (frame_topic , text = 'Proceed' , image = front , relief = "flat" , bg = c1 ,command = topic_1)
button_opt_1.place(x = 350 , y = 120)
option_4 = Label (frame_topic , text = '2- Units of physical Quantities ' , font = 'Verdana 15 italic' , bg = c1 , fg= c2)
option_4.place(x = 30 , y = 240)
button_opt_4 = Button (frame_topic , text = 'Proceed' , image = front , relief = "flat" , bg = c1 ,command = topic_4)
button_opt_4.place(x = 350 , y = 240)
option_5 = Label (frame_topic , text = '3- Input Output Devices ' , font = 'Verdana 15 italic' , bg = c1 , fg= c2)
option_5.place(x = 30 , y = 280)
button_opt_5 = Button (frame_topic , text = 'Proceed' , image = front , relief = "flat" , bg = c1 , command = topic_5)
button_opt_5.place(x = 350 , y = 280)
button_back = Button (frame_topic , image = back , relief = "flat", bg = c1 , command = topic_to_info).place(x=10 , y = 10)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
""" CHAT FRAME """
"""" main chat screen """
frame_chat = Frame (root , bg = c1 , height = '670' , width = '550')
frame_chat.pack_propagate(0)
frame_top = Frame( frame_chat , bg = c3 , height = '100' , width = '550')
frame_top.pack()
label_topic = Label ( frame_top , bg = c3 , fg = 'white' , font = 'Verdana 20 bold ')
label_topic.pack(pady = '40')
frame_spacer = Frame( frame_top , bg = c2 , height = "10" , width = "550" )
frame_spacer.pack()
bottom_frame = Frame (frame_chat , bg = c2 , height = '100' , width = '550')
bottom_frame.pack_propagate(0)
bottom_frame.pack(side = BOTTOM)
button = Button (bottom_frame , image = submit_img , relief = "flat", font = 'Vardana 10 bold' , bg = c3 , command = submit )
button.place(x = 410 , y = 27)
entry = Text (bottom_frame , bg = c3 , fg = c6 , height = '5' , width ='45' , font ='Verdana 10')
entry.bind ('<Return>' , submit)
entry.place(x = 30, y = 10)
frame_chats = Frame (frame_chat , bg = c1 , height = '450' , width = '500' )
frame_chats.pack_propagate (0)
frame_chats.pack()
label_space = Label(frame_chats , bg = c1).pack()
button_refresh = Button (frame_chat , bg = c3 , fg = c2 , text = 'refresh' , font = 'Vardana 10 bold' , command =refresh_screen)
button_refresh.place(x = 440 , y = 80)
button_write = Button (bottom_frame , text = 'write' ,bg = c3 ,fg = c2 , font = 'Vardana 8' , command = write_file )
button_back = Button (frame_chat , image = back , relief = "flat" , bg = c3 , command = chat_to_topic).place(x=10 , y = 10)
button_front = Button (frame_chat , image = exitt , relief = "flat" , bg = c3 , command = root.destroy ).place(x=440 , y = 10)
#-----------------------------------------------------------------------------------------------------------
root.mainloop ()
""" END OF CODE """