开发者

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():
   开发者_JS百科     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))


Since there are potentially many threads writing to the queue (one for every time the button is pressed) it is a little unclear when text should be deleted and new text should be inserted. For example, if text has just been written and new text arrives, should the new text be written immediately or should it be added to a queue for later display as time permits?

You can setup the status handler to process delete commands as well as insert commands. This version of the handler starts a thread after every insert that sends back a delete command. If the ID of the delete command matches the ID of the text currently being displayed, then the status handler erases the display:

def status(t, current_id, queue):
    try:
        data = queue.get(block = False)

        # Insert text for ID command:
        if type(data) == tuple:
            (id, str) = data
            t.delete(0, END)
            t.insert(0, str)
            current_id = id

            # Thread that sends a delete command
            # after a fixed delay.
            make_delete_thread(id, queue)

        # Delete text for ID command:
        elif data == current_id:
            t.delete(0, END)

    except Queue.Empty:
        pass

    t.after(10, lambda: status(t, current_id, queue))

def make_delete_thread(id, queue):
    thread.start_new_thread(delete_thread, (id, queue))

def delete_thread(id, queue):
    time.sleep(1)
    queue.put(id)


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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜