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import tkinter.messagebox
|
| 6 |
+
import pyttsx3
|
| 7 |
+
import threading
|
| 8 |
+
from rihanna import rihanna
|
| 9 |
+
saved_username = ["You"]
|
| 10 |
+
window_size = "400x400"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class ChatInterface(Frame):
|
| 14 |
+
|
| 15 |
+
def __init__(self, master=None):
|
| 16 |
+
Frame.__init__(self, master)
|
| 17 |
+
self.master = master
|
| 18 |
+
|
| 19 |
+
# sets default bg for top level windows
|
| 20 |
+
self.tl_bg = "#EEEEEE"
|
| 21 |
+
self.tl_bg2 = "#EEEEEE"
|
| 22 |
+
self.tl_fg = "#000000"
|
| 23 |
+
self.font = "Verdana 10"
|
| 24 |
+
|
| 25 |
+
menu = Menu(self.master)
|
| 26 |
+
self.master.config(menu=menu, bd=5)
|
| 27 |
+
# Menu bar
|
| 28 |
+
|
| 29 |
+
# File
|
| 30 |
+
file = Menu(menu, tearoff=0)
|
| 31 |
+
menu.add_cascade(label="File", menu=file)
|
| 32 |
+
file.add_command(label="Clear Chat", command=self.clear_chat)
|
| 33 |
+
# file.add_separator()
|
| 34 |
+
file.add_command(label="Exit", command=self.chat_exit)
|
| 35 |
+
|
| 36 |
+
# Options
|
| 37 |
+
options = Menu(menu, tearoff=0)
|
| 38 |
+
menu.add_cascade(label="Options", menu=options)
|
| 39 |
+
|
| 40 |
+
# username
|
| 41 |
+
|
| 42 |
+
# font
|
| 43 |
+
font = Menu(options, tearoff=0)
|
| 44 |
+
options.add_cascade(label="Font", menu=font)
|
| 45 |
+
font.add_command(label="Default", command=self.font_change_default)
|
| 46 |
+
font.add_command(label="Times", command=self.font_change_times)
|
| 47 |
+
font.add_command(label="System", command=self.font_change_system)
|
| 48 |
+
font.add_command(label="Helvetica", command=self.font_change_helvetica)
|
| 49 |
+
font.add_command(label="Fixedsys", command=self.font_change_fixedsys)
|
| 50 |
+
|
| 51 |
+
# color theme
|
| 52 |
+
color_theme = Menu(options, tearoff=0)
|
| 53 |
+
options.add_cascade(label="Color Theme", menu=color_theme)
|
| 54 |
+
color_theme.add_command(label="Default", command=self.color_theme_default)
|
| 55 |
+
# color_theme.add_command(label="Night",command=self.)
|
| 56 |
+
color_theme.add_command(label="Grey", command=self.color_theme_grey)
|
| 57 |
+
color_theme.add_command(label="Blue", command=self.color_theme_dark_blue)
|
| 58 |
+
|
| 59 |
+
color_theme.add_command(label="Torque", command=self.color_theme_turquoise)
|
| 60 |
+
color_theme.add_command(label="Hacker", command=self.color_theme_hacker)
|
| 61 |
+
# color_theme.add_command(label='Mkbhd',command=self.MKBHD)
|
| 62 |
+
|
| 63 |
+
help_option = Menu(menu, tearoff=0)
|
| 64 |
+
menu.add_cascade(label="Help", menu=help_option)
|
| 65 |
+
# help_option.add_command(label="Features", command=self.features_msg)
|
| 66 |
+
help_option.add_command(label="About Rihanna", command=self.msg)
|
| 67 |
+
help_option.add_command(label="Develpoers", command=self.about)
|
| 68 |
+
|
| 69 |
+
self.text_frame = Frame(self.master, bd=6)
|
| 70 |
+
self.text_frame.pack(expand=True, fill=BOTH)
|
| 71 |
+
|
| 72 |
+
# scrollbar for text box
|
| 73 |
+
self.text_box_scrollbar = Scrollbar(self.text_frame, bd=0)
|
| 74 |
+
self.text_box_scrollbar.pack(fill=Y, side=RIGHT)
|
| 75 |
+
|
| 76 |
+
# contains messages
|
| 77 |
+
self.text_box = Text(self.text_frame, yscrollcommand=self.text_box_scrollbar.set, state=DISABLED,
|
| 78 |
+
bd=1, padx=6, pady=6, spacing3=8, wrap=WORD, bg=None, font="Verdana 10", relief=GROOVE,
|
| 79 |
+
width=10, height=1)
|
| 80 |
+
self.text_box.pack(expand=True, fill=BOTH)
|
| 81 |
+
self.text_box_scrollbar.config(command=self.text_box.yview)
|
| 82 |
+
|
| 83 |
+
# frame containing user entry field
|
| 84 |
+
self.entry_frame = Frame(self.master, bd=1)
|
| 85 |
+
self.entry_frame.pack(side=LEFT, fill=BOTH, expand=True)
|
| 86 |
+
|
| 87 |
+
# entry field
|
| 88 |
+
self.entry_field = Entry(self.entry_frame, bd=1, justify=LEFT)
|
| 89 |
+
self.entry_field.pack(fill=X, padx=6, pady=6, ipady=3)
|
| 90 |
+
# self.users_message = self.entry_field.get()
|
| 91 |
+
|
| 92 |
+
# frame containing send button and emoji button
|
| 93 |
+
# self.send_button_frame = Frame(self.master, bd=0)
|
| 94 |
+
# self.send_button_frame.pack(fill=BOTH)
|
| 95 |
+
#
|
| 96 |
+
# # send button
|
| 97 |
+
# self.send_button = Button(self.send_button_frame, text="Send", width=5, relief=GROOVE, bg='white',
|
| 98 |
+
# bd=1, command=lambda: self.send_message_insert(None), activebackground="#FFFFFF",
|
| 99 |
+
# activeforeground="#000000")
|
| 100 |
+
# self.send_button.pack(side=LEFT, ipady=8)
|
| 101 |
+
self.master.bind("<Return>", self.send_message_insert)
|
| 102 |
+
|
| 103 |
+
self.last_sent_label(date="No messages sent.")
|
| 104 |
+
status = ttk.Progressbar(self.entry_frame, orient=HORIZONTAL, length=180, mode='indeterminate')
|
| 105 |
+
status.pack(fill=X, side=BOTTOM)
|
| 106 |
+
status.start()
|
| 107 |
+
# t2 = threading.Thread(target=self.send_message_insert(, name='t1')
|
| 108 |
+
# t2.start()
|
| 109 |
+
|
| 110 |
+
def playResponce(self, responce):
|
| 111 |
+
x = pyttsx3.init()
|
| 112 |
+
|
| 113 |
+
#x.setProperty('rate', 120)
|
| 114 |
+
x.setProperty('volume', 100)
|
| 115 |
+
x.say(responce)
|
| 116 |
+
x.runAndWait()
|
| 117 |
+
|
| 118 |
+
def last_sent_label(self, date):
|
| 119 |
+
|
| 120 |
+
try:
|
| 121 |
+
self.sent_label.destroy()
|
| 122 |
+
except AttributeError:
|
| 123 |
+
pass
|
| 124 |
+
|
| 125 |
+
self.sent_label = Label(self.entry_frame, font="Verdana 7", text=date, bg=self.tl_bg2, fg=self.tl_fg)
|
| 126 |
+
self.sent_label.pack(side=LEFT, fill=X, padx=3)
|
| 127 |
+
|
| 128 |
+
def clear_chat(self):
|
| 129 |
+
self.text_box.config(state=NORMAL)
|
| 130 |
+
self.last_sent_label(date="No messages sent.")
|
| 131 |
+
self.text_box.delete(1.0, END)
|
| 132 |
+
self.text_box.delete(1.0, END)
|
| 133 |
+
self.text_box.config(state=DISABLED)
|
| 134 |
+
|
| 135 |
+
@staticmethod
|
| 136 |
+
def chat_exit():
|
| 137 |
+
exit()
|
| 138 |
+
|
| 139 |
+
@staticmethod
|
| 140 |
+
def msg():
|
| 141 |
+
tkinter.messagebox.showinfo("Rihanna v1.0",
|
| 142 |
+
'Rihanna is a chatbot developed for fun')
|
| 143 |
+
|
| 144 |
+
@staticmethod
|
| 145 |
+
def about():
|
| 146 |
+
tkinter.messagebox.showinfo("Rihanna Developers",
|
| 147 |
+
"Emeka")
|
| 148 |
+
|
| 149 |
+
def send_message_insert(self, message):
|
| 150 |
+
user_input = self.entry_field.get()
|
| 151 |
+
pr1 = "Human : " + user_input + "\n"
|
| 152 |
+
self.text_box.configure(state=NORMAL)
|
| 153 |
+
self.text_box.insert(END, pr1)
|
| 154 |
+
self.text_box.configure(state=DISABLED)
|
| 155 |
+
self.text_box.see(END)
|
| 156 |
+
|
| 157 |
+
ob = self.chat(user_input)
|
| 158 |
+
pr = "Rihanna : " + ob + "\n"
|
| 159 |
+
self.text_box.configure(state=NORMAL)
|
| 160 |
+
self.text_box.insert(END, pr)
|
| 161 |
+
self.text_box.configure(state=DISABLED)
|
| 162 |
+
self.text_box.see(END)
|
| 163 |
+
self.last_sent_label(str(time.strftime("Last message sent: " + '%B %d, %Y' + ' at ' + '%I:%M %p')))
|
| 164 |
+
self.entry_field.delete(0, END)
|
| 165 |
+
time.sleep(0)
|
| 166 |
+
t2 = threading.Thread(target=self.playResponce, args=(ob,))
|
| 167 |
+
t2.start()
|
| 168 |
+
# return ob
|
| 169 |
+
|
| 170 |
+
def font_change_default(self):
|
| 171 |
+
self.text_box.config(font="Verdana 10")
|
| 172 |
+
self.entry_field.config(font="Verdana 10")
|
| 173 |
+
self.font = "Verdana 10"
|
| 174 |
+
|
| 175 |
+
def font_change_times(self):
|
| 176 |
+
self.text_box.config(font="Times")
|
| 177 |
+
self.entry_field.config(font="Times")
|
| 178 |
+
self.font = "Times"
|
| 179 |
+
|
| 180 |
+
def font_change_system(self):
|
| 181 |
+
self.text_box.config(font="System")
|
| 182 |
+
self.entry_field.config(font="System")
|
| 183 |
+
self.font = "System"
|
| 184 |
+
|
| 185 |
+
def font_change_helvetica(self):
|
| 186 |
+
self.text_box.config(font="helvetica 10")
|
| 187 |
+
self.entry_field.config(font="helvetica 10")
|
| 188 |
+
self.font = "helvetica 10"
|
| 189 |
+
|
| 190 |
+
def font_change_fixedsys(self):
|
| 191 |
+
self.text_box.config(font="fixedsys")
|
| 192 |
+
self.entry_field.config(font="fixedsys")
|
| 193 |
+
self.font = "fixedsys"
|
| 194 |
+
|
| 195 |
+
def color_theme_default(self):
|
| 196 |
+
self.master.config(bg="#EEEEEE")
|
| 197 |
+
self.text_frame.config(bg="#EEEEEE")
|
| 198 |
+
self.entry_frame.config(bg="#EEEEEE")
|
| 199 |
+
self.text_box.config(bg="#FFFFFF", fg="#000000")
|
| 200 |
+
self.entry_field.config(bg="#FFFFFF", fg="#000000", insertbackground="#000000")
|
| 201 |
+
# self.send_button_frame.config(bg="#EEEEEE")
|
| 202 |
+
# self.send_button.config(bg="#FFFFFF", fg="#000000", activebackground="#FFFFFF", activeforeground="#000000")
|
| 203 |
+
|
| 204 |
+
self.sent_label.config(bg="#EEEEEE", fg="#000000")
|
| 205 |
+
|
| 206 |
+
self.tl_bg = "#FFFFFF"
|
| 207 |
+
self.tl_bg2 = "#EEEEEE"
|
| 208 |
+
self.tl_fg = "#000000"
|
| 209 |
+
|
| 210 |
+
# Dark
|
| 211 |
+
def color_theme_dark(self):
|
| 212 |
+
self.master.config(bg="#2a2b2d")
|
| 213 |
+
self.text_frame.config(bg="#2a2b2d")
|
| 214 |
+
self.text_box.config(bg="#212121", fg="#FFFFFF")
|
| 215 |
+
self.entry_frame.config(bg="#2a2b2d")
|
| 216 |
+
self.entry_field.config(bg="#212121", fg="#FFFFFF", insertbackground="#FFFFFF")
|
| 217 |
+
# self.send_button_frame.config(bg="#2a2b2d")
|
| 218 |
+
# self.send_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
|
| 219 |
+
# self.emoji_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
|
| 220 |
+
self.sent_label.config(bg="#2a2b2d", fg="#FFFFFF")
|
| 221 |
+
|
| 222 |
+
self.tl_bg = "#212121"
|
| 223 |
+
self.tl_bg2 = "#2a2b2d"
|
| 224 |
+
self.tl_fg = "#FFFFFF"
|
| 225 |
+
|
| 226 |
+
# Grey
|
| 227 |
+
def color_theme_grey(self):
|
| 228 |
+
self.master.config(bg="#444444")
|
| 229 |
+
self.text_frame.config(bg="#444444")
|
| 230 |
+
self.text_box.config(bg="#4f4f4f", fg="#ffffff")
|
| 231 |
+
self.entry_frame.config(bg="#444444")
|
| 232 |
+
self.entry_field.config(bg="#4f4f4f", fg="#ffffff", insertbackground="#ffffff")
|
| 233 |
+
# self.send_button_frame.config(bg="#444444")
|
| 234 |
+
# self.send_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
|
| 235 |
+
# self.emoji_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
|
| 236 |
+
self.sent_label.config(bg="#444444", fg="#ffffff")
|
| 237 |
+
|
| 238 |
+
self.tl_bg = "#4f4f4f"
|
| 239 |
+
self.tl_bg2 = "#444444"
|
| 240 |
+
self.tl_fg = "#ffffff"
|
| 241 |
+
|
| 242 |
+
def color_theme_turquoise(self):
|
| 243 |
+
self.master.config(bg="#003333")
|
| 244 |
+
self.text_frame.config(bg="#003333")
|
| 245 |
+
self.text_box.config(bg="#669999", fg="#FFFFFF")
|
| 246 |
+
self.entry_frame.config(bg="#003333")
|
| 247 |
+
self.entry_field.config(bg="#669999", fg="#FFFFFF", insertbackground="#FFFFFF")
|
| 248 |
+
# self.send_button_frame.config(bg="#003333")
|
| 249 |
+
# self.send_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
|
| 250 |
+
# self.emoji_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
|
| 251 |
+
self.sent_label.config(bg="#003333", fg="#FFFFFF")
|
| 252 |
+
|
| 253 |
+
self.tl_bg = "#669999"
|
| 254 |
+
self.tl_bg2 = "#003333"
|
| 255 |
+
self.tl_fg = "#FFFFFF"
|
| 256 |
+
|
| 257 |
+
# Blue
|
| 258 |
+
|
| 259 |
+
def color_theme_dark_blue(self):
|
| 260 |
+
self.master.config(bg="#263b54")
|
| 261 |
+
self.text_frame.config(bg="#263b54")
|
| 262 |
+
self.text_box.config(bg="#1c2e44", fg="#FFFFFF")
|
| 263 |
+
self.entry_frame.config(bg="#263b54")
|
| 264 |
+
self.entry_field.config(bg="#1c2e44", fg="#FFFFFF", insertbackground="#FFFFFF")
|
| 265 |
+
# self.send_button_frame.config(bg="#263b54")
|
| 266 |
+
# self.send_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
|
| 267 |
+
# self.emoji_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
|
| 268 |
+
self.sent_label.config(bg="#263b54", fg="#FFFFFF")
|
| 269 |
+
|
| 270 |
+
self.tl_bg = "#1c2e44"
|
| 271 |
+
self.tl_bg2 = "#263b54"
|
| 272 |
+
self.tl_fg = "#FFFFFF"
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
# Hacker
|
| 276 |
+
def color_theme_hacker(self):
|
| 277 |
+
self.master.config(bg="#0F0F0F")
|
| 278 |
+
self.text_frame.config(bg="#0F0F0F")
|
| 279 |
+
self.entry_frame.config(bg="#0F0F0F")
|
| 280 |
+
self.text_box.config(bg="#0F0F0F", fg="#33FF33")
|
| 281 |
+
self.entry_field.config(bg="#0F0F0F", fg="#33FF33", insertbackground="#33FF33")
|
| 282 |
+
# self.send_button_frame.config(bg="#0F0F0F")
|
| 283 |
+
# self.send_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
|
| 284 |
+
# self.emoji_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
|
| 285 |
+
self.sent_label.config(bg="#0F0F0F", fg="#33FF33")
|
| 286 |
+
|
| 287 |
+
self.tl_bg = "#0F0F0F"
|
| 288 |
+
self.tl_bg2 = "#0F0F0F"
|
| 289 |
+
self.tl_fg = "#33FF33"
|
| 290 |
+
|
| 291 |
+
# Default font and color theme
|
| 292 |
+
def default_format(self):
|
| 293 |
+
self.font_change_default()
|
| 294 |
+
self.color_theme_default()
|
| 295 |
+
|
| 296 |
+
def chat(self, msg):
|
| 297 |
+
theme = {"Torque": self.color_theme_turquoise, "Hacker": self.color_theme_hacker,
|
| 298 |
+
"Blue": self.color_theme_dark_blue,
|
| 299 |
+
"Grey": self.color_theme_grey, "Default": self.color_theme_default}
|
| 300 |
+
msg = msg.lower().strip()
|
| 301 |
+
if msg[:len('change theme ')] == 'change theme ':
|
| 302 |
+
th = msg[len('change theme '):].capitalize()
|
| 303 |
+
if th in theme:
|
| 304 |
+
theme[th]()
|
| 305 |
+
return f'Theme changed to {th}'
|
| 306 |
+
else:
|
| 307 |
+
return f'cannot find theme {th}'
|
| 308 |
+
else:
|
| 309 |
+
return str(rihanna(msg)['say'])
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
root = Tk()
|
| 314 |
+
|
| 315 |
+
a = ChatInterface(root)
|
| 316 |
+
root.geometry(window_size)
|
| 317 |
+
root.title("Rihanna")
|
| 318 |
+
root.iconbitmap('bird.ico')
|
| 319 |
+
root.mainloop()
|