-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
100 lines (77 loc) · 3.11 KB
/
gui.py
File metadata and controls
100 lines (77 loc) · 3.11 KB
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
import tkinter as tk
from tkinter import PhotoImage
from chatbot import get_response, load_recent_messages
class ChatApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Matrix Chat")
self.geometry("1000x700")
self.configure(bg="black")
self.canvas = tk.Canvas(self, highlightthickness=0)
self.canvas.pack(fill="both", expand=True)
self.bg_image = PhotoImage(file="japanese-neon.png")
self.canvas.create_image(0, 0, image=self.bg_image, anchor="nw")
self.chat_panel = tk.Frame(self, bg="black")
self.scrollbar = tk.Scrollbar(self.chat_panel)
self.scrollbar.pack(side="right", fill="y")
self.output_section = tk.Text(
self.chat_panel,
fg="#008529",
bg="black",
width=100,
height=25,
wrap="word",
yscrollcommand=self.scrollbar.set,
state="disabled"
)
self.output_section.pack(pady=(10, 5))
self.scrollbar.config(command=self.output_section.yview)
self.bottom_frame = tk.Frame(self.chat_panel, bg="black")
self.input_section = tk.Text(
self.bottom_frame,
width=79,
height=5,
bg="black",
fg="#008529"
)
self.input_section.grid(row=0, column=0, padx=5, pady=5)
self.button = tk.Button(
self.bottom_frame,
text="Ask",
width=5,
height=2,
bg="black",
fg="#008529",
command=self.on_submit
)
self.button.grid(row=0, column=1, padx=5, pady=5)
self.bottom_frame.pack(pady=(0, 10))
self.chat_panel_id = self.canvas.create_window(0, 0, window=self.chat_panel)
self.canvas.bind("<Configure>", self.center_widgets)
self.load_chat_history()
def center_widgets(self, event=None):
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
self.canvas.coords(self.chat_panel_id, width / 2, height / 2)
def on_submit(self):
user_input = self.input_section.get("1.0", tk.END).strip()
if user_input:
self.display_output(f"You: {user_input}\n", "user")
response = get_response(user_input)
self.display_output(f"Bot: {response}\n\n", "bot")
self.input_section.delete("1.0", tk.END)
def display_output(self, text, tag):
self.output_section.config(state="normal")
self.output_section.insert(tk.END, text, tag)
self.output_section.config(state="disabled")
self.output_section.see(tk.END)
def load_chat_history(self):
history = load_recent_messages("default_user", limit=100)
self.output_section.config(state="normal")
for role, content in history:
if role == "user":
self.output_section.insert(tk.END, f"You: {content}\n", "user")
else:
self.output_section.insert(tk.END, f"Bot: {content}\n\n", "bot")
self.output_section.config(state="disabled")
self.output_section.see(tk.END)