0

如何证明文本右对齐

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()
4

1 回答 1

2

您可以使用tag_config()选项定义标签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")

然后将此标记分配给您希望它右对齐的行:

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

更新:如果您希望机器人在两秒钟后回复:

if v == 'hai':
    root.after(2000, txt.insert, END, '\nbot ==> hello', 'right')
于 2020-07-29T04:29:13.810 回答