-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interface.py
148 lines (123 loc) · 5.12 KB
/
Interface.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
#python chatwindow program
#Import the library (This whole thing works on tkinker)
from tkinter import *
from tkinter import font
from tkinter import ttk
from datetime import datetime
#from chat import get_response, bot_name
#holdover from "insert name window",
user_name = "You"
BG = "#ffefd5"
BGtext = "#ffffff"
FGtext = "#000000"
class chat_GUI:
def __init__(self):
#create chat window
self.Window = Tk()
self.Setup()
def run(self):
self.Window.mainloop()
def Setup(self):
print ("Window setup")
#building the actual core program window
self.Window.title("Chat program")
self.Window.resizable(width = False,
height = False)
self.Window.configure(width = 470,
height = 550,
bg = BG)
self.Title = Label(self.Window,
bg = BG,
fg = FGtext,
text = "Libot interface V0.4",
font = "Helvetica 13 bold",
pady = 5)
self.Title.place(relwidth = 1)
#chatscreen interface (shows chat to date)
self.chatscreen = Text(self.Window,
width = 20,
height = 2,
bg = BGtext,
fg = FGtext,
font = "Helvetica 14",
padx = 5,
pady = 5)
self.chatscreen.place(relheight = 0.745,
relwidth = 1,
rely = 0.08)
self.chatscreen.configure(cursor="arrow", state=DISABLED)
#chatscreen interface scrollbar (self evidant what its for)
scrollbar = Scrollbar(self.chatscreen)
# place the scroll bar on chatscreen (NOT WINDOW)
scrollbar.place(relheight = 1,
relx = 0.974)
#command so it scrolls the text (y-axis)
scrollbar.config(command = self.chatscreen.yview)
#cosmetic labelling/placement of messenger
self.messengerplace = Label(self.Window,
bg = BG,
height = 80)
self.messengerplace.place(relwidth = 1,
rely = 0.825)
#widget for user to enter text
self.messenger = Entry(self.messengerplace,
bg = BGtext,
fg = FGtext,
font = "Helvetica 13")
#place the enter message widget in the main window
self.messenger.place(relwidth = 0.74,
relheight = 0.06,
rely = 0.008,
relx = 0.011)
#auto focus on the entry message box when the window is active
self.messenger.focus()
#enter command functionality
self.messenger.bind("<Return>", self.entermsg)
#sendbutton coding
sendbutton = Button(self.messengerplace,
text = "Send",
font = "Helvetica 10 bold",
width = 20,
bg = BG,
command = lambda: self.entermsg(None))
sendbutton.place(relx = 0.77,
rely = 0.008,
relheight = 0.06,
relwidth = 0.22)
#function to send if enter is pressed
def entermsg(self, event):
message=self.messenger.get()
self.chat_insert(message, user_name)
#insert from messenger into chatscreen
def chat_insert(self, message, sender):
#if messenger is empty dont trigger
if not message:
return
#clear messenger when message is sent
self.messenger.delete(0, END)
#dump message from user on the end of the chatlog
usermessage = f"{sender}: {message} \n"
self.chatscreen.configure(state=NORMAL)
self.chatscreen.insert(END, usermessage)
self.chatscreen.configure(state=DISABLED)
#bot response debug command
botmessage = "Testing bot: Response!\n"
#chatbot response
#botmessage = f"{bot_name}: {get_response{message}} \n"
self.chatscreen.configure(state=NORMAL)
self.chatscreen.insert(END, botmessage)
self.chatscreen.configure(state=DISABLED)
self.writelog(usermessage, botmessage)
#autoscroll to the end when sending
self.chatscreen.see(END)
#write a chatlog (NOTE: intensive, will be cleaned up to provide greater efficency)
def writelog(self, usermessage, botmessage):
savefile = open("logged conversation.txt", "a")
timestamp = datetime.now()
timestamp = timestamp.strftime("%Y-%m-%d %H:%M:%S")
contents = timestamp + "\n" + usermessage + "\n" + timestamp + "\n" + botmessage + "\n"
savefile.write(contents)
savefile.close()
if __name__ == "__main__":
app = chat_GUI()
app.run()