File size: 3,490 Bytes
7678704 |
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 |
import tkinter as tk
from tkinter import Canvas, Frame, Scrollbar, Label, Entry, BOTH, RIGHT, LEFT, Y, NW
from predict import chatbot_response
class ChatbotGUI:
def __init__(self, root):
self.root = root
self.root.title("LMS Chatbot")
self.root.geometry("500x600")
self.root.minsize(400, 400)
self.root.configure(bg="#f0f0f0")
# Chat frame with canvas for scrolling
self.frame = Frame(root, bg="#f0f0f0")
self.frame.pack(padx=10, pady=10, fill=BOTH, expand=True)
self.canvas = Canvas(self.frame, bg="#f0f0f0", highlightthickness=0)
self.scrollbar = Scrollbar(self.frame, orient="vertical", command=self.canvas.yview)
self.scrollable_frame = Frame(self.canvas, bg="#f0f0f0")
self.scrollable_frame.bind(
"<Configure>",
lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all"))
)
self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor=NW)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side=LEFT, fill=BOTH, expand=True)
self.scrollbar.pack(side=RIGHT, fill=Y)
# Entry box
self.entry = Entry(root, font=("Helvetica", 14), bd=2, relief="groove")
self.entry.pack(padx=10, pady=10, fill="x")
self.entry.bind("<Return>", self.send_message)
# Typing indicator
self.typing_label = Label(root, text="", bg="#f0f0f0", fg="gray", font=("Helvetica", 10))
self.typing_label.pack(pady=(0,5))
def send_message(self, event):
user_input = self.entry.get().strip()
if not user_input:
return
self.entry.delete(0, tk.END)
self.add_message(user_input, sender="user")
self.typing_label.config(text="Bot is typing...")
self.root.after(500, lambda: self.bot_reply(user_input))
def bot_reply(self, user_input):
response = chatbot_response(user_input)
self.typing_label.config(text="")
self.animate_bot_response(response)
def add_message(self, message, sender="bot"):
bubble = Label(self.scrollable_frame, text=message, wraplength=300, justify="left",
bg="#DCF8C6" if sender=="user" else "#FFFFFF",
fg="#000000", font=("Helvetica", 12), padx=10, pady=5, bd=1, relief="solid")
bubble.pack(anchor="e" if sender=="user" else "w", pady=5, padx=5)
self.canvas.update_idletasks()
self.canvas.yview_moveto(1.0)
def animate_bot_response(self, text, idx=0, message=""):
if idx < len(text):
message += text[idx]
if hasattr(self, "bot_bubble"):
self.bot_bubble.config(text=message)
else:
self.bot_bubble = Label(self.scrollable_frame, text=message, wraplength=300, justify="left",
bg="#FFFFFF", fg="#000000", font=("Helvetica", 12), padx=10, pady=5, bd=1, relief="solid")
self.bot_bubble.pack(anchor="w", pady=5, padx=5)
idx += 1
self.root.after(30, self.animate_bot_response, text, idx, message)
else:
del self.bot_bubble
self.canvas.update_idletasks()
self.canvas.yview_moveto(1.0)
if __name__ == "__main__":
root = tk.Tk()
app = ChatbotGUI(root)
root.mainloop()
|