0

How to make the bot's reply to "hello"after 2seconds of saying"hai".? I used sleep(2) inside the sent().but failed.i need when the user text hai it's needed to print and after 2seconds the bot reply hello.. what can I do for getting this as o/p?

from tkinter import *

root=Tk()
ABC =Frame(root,bg='#010101',bd=20,relief=RIDGE)

ABC.grid()

def sent():

  txt =Text(ABC,height=2,width=40,padx=10,pady=10)

  txt.tag_config("right", justify="right")
  txt.pack()
  v=e.get()
  sent="You => " + v
  txt.insert(END,"\n"+sent)

  if v=='hai':
     
    txt =Text(ABC,height=2,width=40,padx=10,pady=10)
    txt.tag_config("right", justify="right")
    txt.pack()
    a='hello'+" <== Bot "
    txt.insert(END,"\n"+a,"right")
    txt.config(state=DISABLED)

   e.delete(0,END)


ABC1=Frame(root,bd=20,)
ABC1.grid()



#txt.tag_configure("green", foreground="green")

#txt.grid(column=0,row=0)


e =Entry(ABC1,width=30,bd=2,insertwidth=5,font=("aril",7))
e.grid(row=1,column=0)

b =Button(ABC1,text='sent',command=sent)
b.grid(row=1,column=1)
root.mainloop()

1 Answers1

1

My answer to your other question already stated that you can use after() to delay the bot reply:

def bot_reply(msg):
    txt =Text(ABC,height=2,width=40,padx=10,pady=10)
    txt.tag_config("right", justify="right")
    txt.pack()
    a=msg+" <== Bot"
    txt.insert(END,"\n"+a,"right")
    txt.config(state=DISABLED)

def sent():

  txt =Text(ABC,height=2,width=40,padx=10,pady=10)

  txt.tag_config("right", justify="right")
  txt.pack()
  v=e.get()
  sent="You => " + v
  txt.insert(END,"\n"+sent)

  if v=='hai':
     root.after(2000, bot_reply, 'hello')

  e.delete(0,END)

But I prefer using Label instead of Text:

def bot_reply(msg):
    reply = msg + " <== Bot"
    Label(ABC, text=reply, height=2, width=40, bd=1, relief='sunken',
          padx=10, pady=10, anchor='e').pack()

def sent():
    v = e.get().strip()
    Label(ABC, text="You => "+v, height=2, width=40, bd=1, relief='sunken',
          padx=10, pady=10, anchor='w').pack()
    if v == 'hai':
        root.after(2000, bot_reply, 'hello')
acw1668
  • 40,144
  • 5
  • 22
  • 34