1

I want to print a message on the UI when the user wishes to exit. This is a part of code relevant to it

if word in quit_words:
    user_to_quit = True
    Print = "Pleasure serving you!"
    print_bot_reply(Print)
    time.sleep(5)
    root.destroy()

The print bot_reply function is as follows:

def print_bot_reply(response):
    response = "\nKalpana: " + str(response)
    label = Label(frame,text = response,bg="#b6efb1", borderwidth=5, relief="raised")
    label.pack(anchor = "w")

The window is closing after 5 seconds, as desired but not displaying the message. Please point me the mistake or suggest some other method to do this

Thanks!

Shivam Singh
  • 1,584
  • 1
  • 10
  • 9
Swappy
  • 11
  • 1
  • 1
    `time.sleep(5)` blocks the kinter mainloop, so your gui isn't updated with the new Label before being destroyed. You can either use `root.update()` right before `time.sleep(5)` (this will still block the gui but shows the Label before blocking) or use `root.after(5000, root.destroy)` to schedule the destroy command to be called after 5 seconds. – fhdrsdg May 31 '18 at 08:03
  • @fhdrsdg root.update works fine. Thanks :) – Swappy May 31 '18 at 08:40
  • @fhdrsdg the command for printing is prior to sleep then why is it not updating it automatically? why do I explicitly have to use `root.update`? – Swappy May 31 '18 at 08:45
  • 1
    For information on the Tkinter mainloop see [this](https://stackoverflow.com/q/29158220/3714930) and [this](https://stackoverflow.com/q/8683217/3714930) question. The mainloop handles the drawing of the widgets, so as long as the program doesn't return to the mainloop nothing is drawn unless you explicitly tell Tkinter to handle all pending events with `root.update()`. – fhdrsdg May 31 '18 at 09:42

0 Answers0