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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tkinter as tk
|
| 2 |
+
from tkinter import scrolledtext, messagebox
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
class ChatApplication:
|
| 8 |
+
def __init__(self, root):
|
| 9 |
+
self.root = root
|
| 10 |
+
self.root.title("Chat Interface")
|
| 11 |
+
self.root.geometry("600x500")
|
| 12 |
+
self.root.configure(bg="#f0f0f0")
|
| 13 |
+
|
| 14 |
+
self.message_history = []
|
| 15 |
+
self.history_file = "chat_history.json"
|
| 16 |
+
|
| 17 |
+
self.load_chat_history()
|
| 18 |
+
self.create_widgets()
|
| 19 |
+
|
| 20 |
+
def create_widgets(self):
|
| 21 |
+
# Main frame
|
| 22 |
+
main_frame = tk.Frame(self.root, bg="#f0f0f0")
|
| 23 |
+
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
|
| 24 |
+
|
| 25 |
+
# Chat display area
|
| 26 |
+
self.chat_display = scrolledtext.ScrolledText(
|
| 27 |
+
main_frame,
|
| 28 |
+
wrap=tk.WORD,
|
| 29 |
+
width=60,
|
| 30 |
+
height=20,
|
| 31 |
+
font=("Arial", 10),
|
| 32 |
+
state=tk.DISABLED
|
| 33 |
+
)
|
| 34 |
+
self.chat_display.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
|
| 35 |
+
|
| 36 |
+
# Input frame
|
| 37 |
+
input_frame = tk.Frame(main_frame, bg="#f0f0f0")
|
| 38 |
+
input_frame.pack(fill=tk.X)
|
| 39 |
+
|
| 40 |
+
# User input field
|
| 41 |
+
self.user_input = tk.Entry(
|
| 42 |
+
input_frame,
|
| 43 |
+
font=("Arial", 12),
|
| 44 |
+
relief=tk.FLAT,
|
| 45 |
+
bg="white"
|
| 46 |
+
)
|
| 47 |
+
self.user_input.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
|
| 48 |
+
self.user_input.bind("<Return>", self.send_message)
|
| 49 |
+
|
| 50 |
+
# Send button
|
| 51 |
+
send_button = tk.Button(
|
| 52 |
+
input_frame,
|
| 53 |
+
text="Send",
|
| 54 |
+
command=self.send_message,
|
| 55 |
+
bg="#4CAF50",
|
| 56 |
+
fg="white",
|
| 57 |
+
font=("Arial", 10, "bold"),
|
| 58 |
+
relief=tk.FLAT,
|
| 59 |
+
padx=20
|
| 60 |
+
)
|
| 61 |
+
send_button.pack(side=tk.RIGHT)
|
| 62 |
+
|
| 63 |
+
# Load existing messages
|
| 64 |
+
self.display_messages()
|
| 65 |
+
|
| 66 |
+
# Focus on input field
|
| 67 |
+
self.user_input.focus_set()
|
| 68 |
+
|
| 69 |
+
def send_message(self, event=None):
|
| 70 |
+
message = self.user_input.get().strip()
|
| 71 |
+
if not message:
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 75 |
+
message_data = {
|
| 76 |
+
"timestamp": timestamp,
|
| 77 |
+
"message": message,
|
| 78 |
+
"sender": "user"
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
self.message_history.append(message_data)
|
| 82 |
+
self.display_message(message_data)
|
| 83 |
+
self.user_input.delete(0, tk.END)
|
| 84 |
+
|
| 85 |
+
# Simulate bot response (in real app, this would be your AI/chat logic)
|
| 86 |
+
self.simulate_bot_response(message)
|
| 87 |
+
|
| 88 |
+
self.save_chat_history()
|
| 89 |
+
|
| 90 |
+
def simulate_bot_response(self, user_message):
|
| 91 |
+
# Simple response simulation - replace with actual AI/chat logic
|
| 92 |
+
responses = {
|
| 93 |
+
"hello": "Hello! How can I help you today?",
|
| 94 |
+
"hi": "Hi there! What can I do for you?",
|
| 95 |
+
"help": "I'm here to help! What do you need assistance with?",
|
| 96 |
+
"bye": "Goodbye! Have a great day!"
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
response = responses.get(user_message.lower(),
|
| 100 |
+
f"I received your message: '{user_message}'. How can I assist you further?")
|
| 101 |
+
|
| 102 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 103 |
+
response_data = {
|
| 104 |
+
"timestamp": timestamp,
|
| 105 |
+
"message": response,
|
| 106 |
+
"sender": "bot"
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
self.message_history.append(response_data)
|
| 110 |
+
self.display_message(response_data)
|
| 111 |
+
self.save_chat_history()
|
| 112 |
+
|
| 113 |
+
def display_message(self, message_data):
|
| 114 |
+
self.chat_display.config(state=tk.NORMAL)
|
| 115 |
+
|
| 116 |
+
# Configure tags for different senders
|
| 117 |
+
self.chat_display.tag_configure("user", foreground="blue", justify="right")
|
| 118 |
+
self.chat_display.tag_configure("bot", foreground="green", justify="left")
|
| 119 |
+
self.chat_display.tag_configure("timestamp", foreground="gray", font=("Arial", 8))
|
| 120 |
+
|
| 121 |
+
# Insert message with appropriate formatting
|
| 122 |
+
if message_data["sender"] == "user":
|
| 123 |
+
self.chat_display.insert(tk.END, f"{message_data['timestamp']}\n", "timestamp")
|
| 124 |
+
self.chat_display.insert(tk.END, f"You: {message_data['message']}\n\n", "user")
|
| 125 |
+
else:
|
| 126 |
+
self.chat_display.insert(tk.END, f"{message_data['timestamp']}\n", "timestamp")
|
| 127 |
+
self.chat_display.insert(tk.END, f"Bot: {message_data['message']}\n\n", "bot")
|
| 128 |
+
|
| 129 |
+
self.chat_display.config(state=tk.DISABLED)
|
| 130 |
+
self.chat_display.see(tk.END)
|
| 131 |
+
|
| 132 |
+
def display_messages(self):
|
| 133 |
+
self.chat_display.config(state=tk.NORMAL)
|
| 134 |
+
self.chat_display.delete(1.0, tk.END)
|
| 135 |
+
|
| 136 |
+
for message in self.message_history:
|
| 137 |
+
if message["sender"] == "user":
|
| 138 |
+
self.chat_display.insert(tk.END, f"{message['timestamp']}\n", "timestamp")
|
| 139 |
+
self.chat_display.insert(tk.END, f"You: {message['message']}\n\n", "user")
|
| 140 |
+
else:
|
| 141 |
+
self.chat_display.insert(tk.END, f"{message['timestamp']}\n", "timestamp")
|
| 142 |
+
self.chat_display.insert(tk.END, f"Bot: {message['message']}\n\n", "bot")
|
| 143 |
+
|
| 144 |
+
self.chat_display.config(state=tk.DISABLED)
|
| 145 |
+
self.chat_display.see(tk.END)
|
| 146 |
+
|
| 147 |
+
def load_chat_history(self):
|
| 148 |
+
try:
|
| 149 |
+
if os.path.exists(self.history_file):
|
| 150 |
+
with open(self.history_file, 'r') as f:
|
| 151 |
+
self.message_history = json.load(f)
|
| 152 |
+
except (FileNotFoundError, json.JSONDecodeError):
|
| 153 |
+
self.message_history = []
|
| 154 |
+
|
| 155 |
+
def save_chat_history(self):
|
| 156 |
+
try:
|
| 157 |
+
with open(self.history_file, 'w') as f:
|
| 158 |
+
json.dump(self.message_history, f, indent=2)
|
| 159 |
+
except Exception as e:
|
| 160 |
+
messagebox.showerror("Error", f"Failed to save chat history: {str(e)}")
|
| 161 |
+
|
| 162 |
+
def main():
|
| 163 |
+
root = tk.Tk()
|
| 164 |
+
app = ChatApplication(root)
|
| 165 |
+
root.mainloop()
|
| 166 |
+
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
main()
|