0

how to justify text alignment to right

from tkinter import *
#from time import *
root=Tk()
#root.geometry('2000x800')
ABC=Frame(root,bg='#1f5629',bd=20,     relief=RIDGE)
ABC.grid()
def sent():
    v=e.get()
    sent="You => " + v
    txt.insert(END,"\n"+sent)

    if v=='hai':
        a="bot ==>"+'hello'
        txt.insert(END,"\n"+a)

    e.delete(0,END)


ABC1=Frame(root,bg='#1f5629',bd=20,)
ABC1.grid()
txt=Text(ABC,height=30,width=40,   padx=10,pady=10)
txt.grid(column=0,row=0)
e=Entry(ABC,width=30)
e.grid(row=1,column=0)
b=Button(ABC1,text='sent',   command=sent)
b.grid(row=1,column=0)
root.mainloop()
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Do you want _all_ text to be right-justified, or only _some_ text? Have you experimented with the various options available to you in the text widget, such as configuring and applying tags to the data? – Bryan Oakley Jul 28 '20 at 23:11
  • Only the reply from the bot needs to be align in right – AVISHITH PM Jul 29 '20 at 18:06

1 Answers1

2

You can use tag_config() to define a tag with option justify="right":

txt = Text(ABC, height=30, width=40, padx=10, pady=10)
txt.grid(column=0, row=0)
txt.tag_config("right", justify="right")

then assign this tag to the line you want it to be right-justified:

if v == 'hai':
    a = "bot ==> "+'hello'
    txt.insert(END, "\n"+a, "right") # apply the "right" tab effect 

Update: If you want the bot reply after two seconds:

if v == 'hai':
    root.after(2000, txt.insert, END, '\nbot ==> hello', 'right')
acw1668
  • 40,144
  • 5
  • 22
  • 34