我只是在学习 Tkinter 和 Python。我正在尝试设计一个聊天机器人。在设计聊天机器人时,我写了一个现在溢出的响应。显然,在这里可以使用 '\n' 对其进行分解,但我将从 json 文件中获取长响应,或者用户可以发送长查询。那么,我能做些什么使文本保留在窗口中并且不会溢出?
任何进一步的建议总是受欢迎的。
这是我的输出。
这是我的代码:
from datetime import datetime
from tkinter import *
import tkinter
import tkinter.scrolledtext as ScrolledText
def send():
ChatLog.tag_configure('tag-left', justify='left')
ChatLog.tag_configure('tag-right', justify='right')
ChatLog.config(state=NORMAL)
frame1 = Frame(ChatLog, bg="#d0ffff")
Label(
frame1,
text="You: What you can do?",
font=(
"Arial",
11),
bg="#d0ffff").grid(
row=0,
column=0,
sticky="w",
padx=5,
pady=5)
Label(
frame1,
text=datetime.now().strftime("%H:%M"),
font=(
"Arial",
7),
bg="#d0ffff").grid(
row=1,
column=0,
sticky="e")
frame2 = Frame(ChatLog, bg="#ffffd0")
Label(
frame2,
text="Bot: I can guide you through Adverse drug reaction list, Blood pressure tracking, Hospitals and Pharmacies",
font=(
"Arial",
11),
bg="#ffffd0").grid(
row=0,
column=0,
sticky="w",
padx=5,
pady=5)
Label(
frame2,
text=datetime.now().strftime("%H:%M"),
font=(
"Arial",
7),
bg="#ffffd0").grid(
row=1,
column=0,
sticky="e")
ChatLog.insert('end', '\n ')
ChatLog.window_create('end', window=frame1)
ChatLog.tag_add("tag-right", "end-1c linestart", "end-1c lineend")
ChatLog.insert('end', '\n ')
ChatLog.window_create('end', window=frame2)
ChatLog.config(state=DISABLED)
base = Tk()
base.title("Chatbot")
base.geometry("400x500")
base.resizable(width=FALSE, height=FALSE)
base.configure(bg='white')
ChatLog = ScrolledText.ScrolledText(
base,
bd=0,
bg="white",
height="8",
width="50",
font="Arial")
ChatLog.config(state=DISABLED)
SendButton = Button(
base,
font=(
"Verdana",
12,
'bold'),
text="Send",
width="8",
height=5,
bd=0,
bg="#fd94b4",
activebackground="#ff467e",
fg='#ffffff',
command=send)
EntryBox = Text(base, bd=0, bg="white", width="29", height="5", font="Arial")
ChatLog.place(x=6, y=6, height=386, width=383)
EntryBox.place(x=6, y=401, height=90, width=265)
SendButton.place(x=275, y=401, height=90)
base.mainloop()