Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does it take time to delete the text in tk.Text in tkinter python?

In the python code below, I’m using message_entry.delete(1.0, tk.END) to delete the text that was entered into the text widget. The problem is that it waits 5 seconds before the text is deleted. I’m aware that this is because of time.sleep(5) in the script below.

Thing is, message_entry.delete(1.0, tk.END) command is above this sleep command, so it should execute before this sleep command and delete the text immediately. Why is it waiting? I want to delete the text as soon as the message is extracted.

            import tkinter as tk
            from tkinter import ttk
            from datetime import datetime
            from PIL import Image, ImageTk
            import time

            def clear_default_text(event):
                message = message_entry.get(1.0, tk.END) 
                ask = "Ask me anything..."
                if ask in message:
                    message_entry.delete(1.0, tk.END)

            def send_message(event=None):
                message = message_entry.get(1.0, "end-1c") 
                message = message.strip()
                message_entry.delete(1.0, tk.END)
                
                if not message:
                    pass 
                else:
                    time.sleep(5)            


            root = tk.Tk()

            root.title("Chat")

            # Maximize the window
            root.attributes('-zoomed', True)

            style = ttk.Style()
            style.theme_use("clam")


            message_entry = tk.Text(root, padx=17, insertbackground='white', width=70, height=1, spacing1=20, spacing3=20, font=('Open Sans', 14))
            message_entry.pack(side=tk.LEFT, padx=(500, 0), pady=(0, 70))  
            #message_entry.insert(0, "Ask me anything...")
            message_entry.insert(1.0, "Ask me anything...")
            message_entry.mark_set("insert", "%d.%d" % (0,0))
            message_entry.bind("<Key>", clear_default_text)  # Bind the Key event
            message_entry.bind("<Return>", send_message)
            #message_entry.bind("<Button-1>", click)

            message_entry.focus_set()  

            root.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add this line after message_entry.delete(1.0, tk.END):

message_entry.update()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading