Search code examples
pythonmultithreadingtkintermessage-queuetkinter-entry

Clear Tkinter Entry text - so new text looks new?


I want to modify the code below to clear the Entry text prior to new text being written. Basically I want to delete text, wait one second, then write new text. This should give the appearance of "NEW" text being written. Any ideas? TIA - Brad

    import thread, Queue, time, random, poster
    from Tkinter import *

    dataQueue = Queue.Queue()

    def status(t):
        try:
            data = dataQueue.get(block=False)
        except Queue.Empty:
            pass
        else:
            t.delete(0, END)
            time.sleep(1)
            t.insert(0, '%s\n' % str(data))
        t.after(2, lambda: status(t))

    def makethread():
        thread.start_new_thread(poster.poster, (1,dataQueue))    

    if __name__ == '__main__':
        root = Tk()
        root.geometry("240x45")
        t = Entry(root)
        t.pack(side=TOP, fill=X)
        Button(root, text='Start Epoch Display',
                command=makethread).pack(side=BOTTOM, fill=X)
        status(t)
        root.mainloop()

In another file called poster

    import random, time

    def poster(id,que):
        while True:
            delay=random.uniform(5, 10)
            time.sleep(delay)
            que.put(' epoch=%f, delay=%f' % (time.time(), delay))

Solution

  • Made these changes and it works... Thanks to @anonakos. See my comments to his answer.

    Main code:
        else:
            t.delete(0, END)
            time.sleep(1)
            t.insert(0, '%s\n' % str(data))
        t.after(2, lambda: status(t))
    
    Poster code:
    def poster(id,que):
        while True:
            delay=random.uniform(5, 10)
            time.sleep(delay-0.5)
            que.put(' ')
            time.sleep(.5)
            que.put(' epoch=%f, delay=%f' % (time.time(), delay))