0

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 to 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

Verohomie
  • 123
  • 1
  • 8

2 Answers2

1

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.

Verohomie
  • 123
  • 1
  • 8
0

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)

Matt Warren
  • 669
  • 8
  • 18
  • Matt- I want to display text while the function is running. The function POSTs energy data and should run for years, or as long as the hardware works. I have been able to display text after the function ends BUT that defeats the real-time status update. – Verohomie Aug 24 '11 at 21:12
  • I found http://stackoverflow.com/questions/5048082 but I don't think queues will handle the real-time nature of this problem. I also found http://stackoverflow.com/questions/6588141 but @msw writes that he knows NO known solution. – Verohomie Aug 24 '11 at 21:35