我正在创建一个聊天机器人,但其工作正常,但我希望文本左右左右,就像whatsapp,messanger等,其中聊天有两个方面供我们使用,第二个方面供我们发送,如果可能的话,每个方面的背景左右文本(不是全部/主要)应具有不同的颜色。 如果有人对此提供帮助,我将非常感谢。
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
import tkinter as tk
try:
import ttk as ttk
import ScrolledText
except ImportError:
import tkinter.ttk as ttk
import tkinter.scrolledtext as ScrolledText
import time
class TkinterGUIExample(tk.Tk):
def __init__(self, *args, **kwargs):
"""
Create & set window variables.
"""
tk.Tk.__init__(self, *args, **kwargs)
self.chatbot = ChatBot(
"GUI Bot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
logic_adapters=[{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand.',
'maximum_similarity_threshold': 1.0
} ]
)
for files in os.listdir('C:/Users/HP/Desktop/FYP BOT/training_data/'):
con=open('C:/Users/HP/Desktop/FYP BOT/training_data/'+files,'r').readlines()
trainer = ListTrainer(self.chatbot)
trainer.train(con)
self.title("Chatterbot")
self.initialize()
def initialize(self):
"""
Set window layout.
"""
self.grid()
ttk.Style().configure("TButton", padding=6, relief="flat",background="#ccc")
style = ttk.Style()
style.map("C.TButton",
foreground=[('pressed', 'red'), ('active', 'blue')],
background=[('pressed', '!disabled', 'black'), ('active', 'white')]
)
self.respond = ttk.Button(self, text='Get Response', command=self.get_response,style="C.TButton")
self.respond.grid(column=1, row=2, sticky='nesw', padx=3, pady=10)
self.usr_input = tk.Entry(self, state='normal',text='Enter your query here!')
self.usr_input.grid(column=0, row=2, sticky='nesw', padx=1, pady=5)
self.conversation_lbl = tk.Label(self, anchor=tk.E, text='Conversation',font=('Arial Bold Underline',18),bg="#f89406",fg="#feffff")
self.conversation_lbl.grid(column=0, row=0,columnspan=2, padx=3, pady=3)
self.conversation = ScrolledText.ScrolledText(self,
state='disabled',borderwidth=5,
highlightthickness=1,
bg='#15202b',fg='#ffffff',
font=('Arial Bold',8))
self.conversation.grid(column=0, row=1, columnspan=2, sticky='nesw', padx=3, pady=3)
def get_response(self):
"""
Get a response from the chatbot and display it.
"""
user_input = self.usr_input.get()
self.usr_input.delete(0, tk.END)
response = self.chatbot.get_response(user_input)
self.conversation['state'] = 'normal'
self.conversation.insert(
tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n"
)
self.conversation['state'] = 'disabled'
time.sleep(0.5)
gui_example = TkinterGUIExample()
gui_example.geometry('930x600+10+10')
gui_example.configure(background='#3a8fc5')
gui_example.mainloop()