-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
79 lines (61 loc) · 2.68 KB
/
app.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
from tkinter import *
from chat import get_response, bot_name
BG_COLOR = "#5e72e4" # Blue
TEXT_COLOR = "#ffffff" # White
FONT = "Helvetica 12"
FONT_BOLD = "Helvetica 12 bold"
MAUVE_COLOR = "#DABEBB" # Rich Mauve
class ChatApplication:
def __init__(self):
self.window = Tk()
self._setup_main_window()
def run(self):
self.window.mainloop()
def _setup_main_window(self):
self.window.title("Chatbot")
self.window.geometry("470x550")
self.window.configure(bg=BG_COLOR)
# Head label
head_label = Label(self.window, bg=BG_COLOR, fg=TEXT_COLOR, text="Chatbot", font=FONT_BOLD, pady=10)
head_label.pack(fill="x")
# Tiny divider
line = Label(self.window, width=450, bg=TEXT_COLOR)
line.pack(fill="x")
# Text widget
self.text_widget = Text(self.window, width=20, height=2, bg=BG_COLOR, fg=TEXT_COLOR, font=FONT, padx=5, pady=5)
self.text_widget.pack(fill="both", expand=True)
self.text_widget.configure(cursor="arrow", state=DISABLED)
# Scrollbar
scrollbar = Scrollbar(self.text_widget)
scrollbar.pack(side="right", fill="y")
scrollbar.configure(command=self.text_widget.yview)
# Bottom label
bottom_label = Label(self.window, bg=BG_COLOR, height=80)
bottom_label.pack(fill="x")
# Message entry box
self.msg_entry = Entry(bottom_label, bg="#ffffff", fg=BG_COLOR, font=FONT)
self.msg_entry.pack(side="left", fill="both", expand=True, padx=5, pady=5)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self._on_enter_pressed)
# Send button with rich mauve background
send_button = Button(bottom_label, text="Send", font=FONT_BOLD, width=20, bg=MAUVE_COLOR, command=lambda: self._on_enter_pressed(None))
send_button.pack(side="right", fill="both", expand=True, padx=5, pady=5)
def _on_enter_pressed(self, event):
msg = self.msg_entry.get()
self._insert_message(msg, "You")
def _insert_message(self, msg, sender):
if not msg:
return
self.msg_entry.delete(0, END)
msg1 = f"{sender}: {msg}\n\n"
self.text_widget.configure(state=NORMAL)
self.text_widget.insert(END, msg1)
self.text_widget.configure(state=DISABLED)
msg2 = f"{bot_name}: {get_response(msg)}\n\n"
self.text_widget.configure(state=NORMAL)
self.text_widget.insert(END, msg2)
self.text_widget.configure(state=DISABLED)
self.text_widget.see(END)
if __name__ == "__main__":
app = ChatApplication()
app.run()