Python Tkinter滚动文本
我正在尝试将scrolledtext中右侧的用户输入(使用tkinter python)与左侧的chatbot输出对齐,但没有成功。 此外,文本背景应为浅色。我的意思是,它应该看起来像messanger或其他信息平台,双方都有不同的方面,有时也有不同的颜色。 请帮助我,这是我最后一年项目的一部分。 我很担心。 这是我的密码:Python Tkinter滚动文本,python,tkinter,alignment,chatbot,Python,Tkinter,Alignment,Chatbot,我正在尝试将scrolledtext中右侧的用户输入(使用tkinter python)与左侧的chatbot输出对齐,但没有成功。 此外,文本背景应为浅色。我的意思是,它应该看起来像messanger或其他信息平台,双方都有不同的方面,有时也有不同的颜色。 请帮助我,这是我最后一年项目的一部分。 我很担心。 这是我的密码: from chatterbot import ChatBot from chatterbot.trainers import ListTrainer import os i
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()
您可以使用
标记
为部分文本指定属性
使用justify
可以在右侧显示文本,但所有行都将向右对齐
import tkinter as tk
import tkinter.scrolledtext as ScrolledText
root = tk.Tk()
#text_widget = tk.Text()
text_widget = ScrolledText.ScrolledText()
text_widget.pack(fill='both', expand=True)
text_widget.tag_configure('tag-center', justify='center')
text_widget.tag_configure('tag-left', justify='left')
text_widget.tag_configure('tag-right', justify='right')
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-center')
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-left')
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-right')
root.mainloop()
为了更好地控制,您可以尝试将画布
与标签
和滚动条
编辑:使用
窗口创建
可以将标签
添加到滚动文本
中,使用上一个标记可以将其向右移动。然后您可以更改标签
-颜色、边距(padx、pady)等。您可以尝试使用图像添加标签
,甚至添加框架
,以放置更多不同的小部件
import tkinter as tk
import tkinter.scrolledtext as ScrolledText
root = tk.Tk()
#text_widget = tk.Text()
text_widget = ScrolledText.ScrolledText()
text_widget.pack(fill='both', expand=True)
label1 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#d0ffff', justify='left', padx=10, pady=5)
label2 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#ffffd0', justify='left', padx=10, pady=5)
label3 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#d0ffff', justify='left', padx=10, pady=5)
text_widget.tag_configure('tag-left', justify='left')
text_widget.tag_configure('tag-right', justify='right')
text_widget.insert('end', '\n')
text_widget.window_create('end', window=label1)
text_widget.insert('end', '\n ', 'tag-right') # space to move Label to the right
text_widget.window_create('end', window=label2)
text_widget.insert('end', '\n')
text_widget.window_create('end', window=label3)
root.mainloop()
您可以使用
标记
为部分文本指定属性
使用justify
可以在右侧显示文本,但所有行都将向右对齐
import tkinter as tk
import tkinter.scrolledtext as ScrolledText
root = tk.Tk()
#text_widget = tk.Text()
text_widget = ScrolledText.ScrolledText()
text_widget.pack(fill='both', expand=True)
text_widget.tag_configure('tag-center', justify='center')
text_widget.tag_configure('tag-left', justify='left')
text_widget.tag_configure('tag-right', justify='right')
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-center')
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-left')
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-right')
root.mainloop()
为了更好地控制,您可以尝试将画布
与标签
和滚动条
编辑:使用
窗口创建
可以将标签
添加到滚动文本
中,使用上一个标记可以将其向右移动。然后您可以更改标签
-颜色、边距(padx、pady)等。您可以尝试使用图像添加标签
,甚至添加框架
,以放置更多不同的小部件
import tkinter as tk
import tkinter.scrolledtext as ScrolledText
root = tk.Tk()
#text_widget = tk.Text()
text_widget = ScrolledText.ScrolledText()
text_widget.pack(fill='both', expand=True)
label1 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#d0ffff', justify='left', padx=10, pady=5)
label2 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#ffffd0', justify='left', padx=10, pady=5)
label3 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#d0ffff', justify='left', padx=10, pady=5)
text_widget.tag_configure('tag-left', justify='left')
text_widget.tag_configure('tag-right', justify='right')
text_widget.insert('end', '\n')
text_widget.window_create('end', window=label1)
text_widget.insert('end', '\n ', 'tag-right') # space to move Label to the right
text_widget.window_create('end', window=label2)
text_widget.insert('end', '\n')
text_widget.window_create('end', window=label3)
root.mainloop()
若要对齐,您可能需要添加空格-并且您必须自己计算要添加的空格数。检查
标记
与对齐
若要对齐,您可能需要添加空格-并且您必须自己计算要添加的空格数。同时检查标记
与对齐
谢谢Lotttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt你能帮我完成吗?我的意思是,我如何在我的代码中实现它,因为我用你的答案做了很多尝试,但它仍然给我错误。这是我最后一年项目的一部分,我是初学者,所以如果你能在你的系统上运行并给我答案,我将非常感谢你