I am just learning Tkinter and Python. I am trying to design a chatbot. While designing the chatbot I wrote a response which is now overflowing. Obviously here it can be broken down using '\n' but I will be fetching long responses from json file or a user can send long inquires. So, What I can do so that text remain in the window and does not overflow?
Any further suggestion is always welcome.
Here is my output.
Here is my code:
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()
While it's possible to configure the label so that it will wrap, this is one of the rare cases where a
Message
widget might be more appropriate since it automatically supports wrapping and justification.For more information about how Label and Message widgets differ, see Message and Label Difference? (tkinter)
Example: