How to change text alignment of chatbot in python using CHATTERBOT [closed]

∥☆過路亽.° 提交于 2020-02-08 10:08:54

问题


i'am creating chatbot using

  1. chatterbot
  2. Tkinter

in python it was working fine before changing in def get_response(self) but both the input and output were on left side as shown in this SS so i tried to align input and output left and right for that i added the labels in in get_response() method and set their alignment after this change it shows me error and changed the behaviour of bot. i'll be very thankful and pray for you if anyone help me. Thanks


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': 0.75
} ]
        )


        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,
                                         text='English',
                                         anchor='center',
                                         font=('Arial Bold ',18),
                                         bg="black",
                                         fg="white")
        self.conversation_lbl.grid(column=0, row=0,columnspan=2, padx=3, pady=3,sticky='news')
        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.
        """
        print('Welcome to chatbot')
        user_input1 = self.usr_input.get()
        user_input = tk.Label(self.conversation, text=user_input1, background='#d0ffff', justify='left', padx=10, pady=5)
        self.conversation.tag_configure('tag-left', justify='left')

        self.conversation.insert('end', '\n')
        self.conversation.window_create('end', window=user_input)

        self.usr_input.delete(0, tk.END)

        response1 = self.chatbot.get_response(user_input,justify='right')
        response = tk.Label(self.conversation, text=response1, background='#d0ffff', justify='left', padx=10, pady=5)
        self.conversation.tag_configure('tag-right', justify='right')

        self.conversation.insert('end', '\n ', 'tag-right') # space to move Label to the right 
        self.conversation.window_create('end', window=response)

        self.conversation['state'] = 'normal'
        self.conversation.insert(
            tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n\n\n"
        )
        self.conversation['state'] = 'disabled'

        time.sleep(0.5)


gui_example = TkinterGUIExample()
gui_example.geometry('810x570+460+80')
gui_example.resizable(0, 0)
gui_example.configure(background='#3a8fc5')
gui_example.mainloop()

来源:https://stackoverflow.com/questions/59992576/how-to-change-text-alignment-of-chatbot-in-python-using-chatterbot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!