-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContestBot.py
102 lines (81 loc) · 3.11 KB
/
ContestBot.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
from tkinter import *
from BotConfig import bot_name,get_response
# ANSI escape codes for colors
RED = '\033[91m'
GREEN = '\033[92m'
BLUE = '\033[94m'
RESET = '\033[0m' # Reset color to default
BG_GRAY = "#A9A9A9"
BG_COLOR = "BLACK" # Fix: Add '#' at the beginning
TEXT_COLOR = "White"
FONT = "Helvetica 14"
FONT_BOLD = "Helvetica 13 bold"
class ChatApp:
def __init__ (self):
self.window = Tk ()
self._setup_main_window ()
def _setup_main_window (self):
self.window.title ("ContestBot")
self.window.configure (width=500, height=550, bg=BG_COLOR)
# head label
head_label = Label(self.window,bg=BG_COLOR,fg=TEXT_COLOR,
text="Hi there!",font=FONT_BOLD,pady=10)
head_label.place(relwidth=1)
# small dividor
line = Label(self.window,width=450,bg=BG_GRAY)
line.place(relwidth=1,rely=0.07,relheight=0.012)
# 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.place(relheight=0.745,relwidth=1,rely=0.08)
self.text_widget.configure(cursor="arrow", state=DISABLED)
# scroll bar
scrollbar = Scrollbar(self.text_widget)
scrollbar.place(relheight=1,relx=0.974)
scrollbar.configure(command=self.text_widget.yview)
# bottom label
bottom_label = Label(self.window,bg=BG_GRAY,height=80)
bottom_label.place(relwidth=1,rely=0.825)
# message entry box
self.msg_entry = Entry(bottom_label,bg="BLACK",fg=TEXT_COLOR,font=FONT,insertwidth=4,insertbackground="white")
self.msg_entry.place(relwidth=0.74, relheight=0.06,rely=0.008,relx=0.011)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self._on_enter_pressed)
# send button
send_button = Button(bottom_label, text="Send", font=FONT_BOLD,width=20,bg=BG_GRAY,
command=lambda: self._on_enter_pressed(None))
send_button.place(relx=0.77, rely=0.008,relheight=0.06,relwidth=0.22)
def _on_enter_pressed(self,event):
msg = self.msg_entry.get().strip()
self._insert_message(msg, "You")
def _insert_message(self,msg,sender):
if not msg:
return
# if the user wants to clear the text widget
elif msg.lower() == "clear" or msg.lower() == "clr":
# Clear the text widget
self.msg_entry.delete(0,END)
self.text_widget.configure (state=NORMAL)
self.text_widget.delete ("1.0", END)
self.text_widget.configure (state=DISABLED)
# if the user wants to close the program
elif msg.lower() == "close" or msg.lower() == "exit" or msg.lower() == "shutdown" or msg.lower() == "shut down":
self._close_window()
else:
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)
def _close_window(self):
self.window.destroy()
def run (self):
self.window.mainloop ()
if __name__ == "__main__":
chatApp = ChatApp ()
chatApp.run ()