开发者

Displaying real-time text in Tkinter after Button command calls function?

I am trying to display real-time text in Tkinter after a Button command calls a function. The function should display a "timestamp" when PycURL receives "HTTP/1.1 200 OK". The function POSTs energy data t开发者_运维技巧o a server every 2 minutes.

Pseudocode, a basic example, and/or general discussion should get me headed in the right direction. I have got the energy data POSTing OK. Now I need to get the GUI working.

Thanks - Brad


Found the answer in the book "Programming Python" by Mark Lutz. The following code is adapted from the book using info from Threads and Queues!

    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)
            t.insert('0', '%s\n' % str(data))
        t.after(250, 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(0.1, .11)
            time.sleep(delay)
            que.put(' epoch=%f, delay=%f' % (time.time(), delay))

This worked.


How are you trying to display real-time text in Tkinter after a button calls a function? What I get so far is, user presses button, function starts and every two minutes posts some data to a server, and tries to display some text somewhere after each post, but has a problem?

How does it try to display the text, and what's the problem?

(I dont know PycURL, so apologies if that would make everything clear)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜