Tkinter listener thread?

G

gregarican

I have a Python UDP listener socket that waits for incoming data. The
socket runs as an endless loop. I would like to pop the incoming data
into an existing Tkinter app that I have created. What's the
easiest/most efficient way of handling this? Would I create a separate
thread that has the listener set a certain Tkinter variable if there is
incoming data? Any suggestions would be tremendously appreciated :)
 
S

Steve Holden

gregarican said:
I have a Python UDP listener socket that waits for incoming data. The
socket runs as an endless loop. I would like to pop the incoming data
into an existing Tkinter app that I have created. What's the
easiest/most efficient way of handling this? Would I create a separate
thread that has the listener set a certain Tkinter variable if there is
incoming data? Any suggestions would be tremendously appreciated :)

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965
 
G

Grant Edwards


That seems a bit complex. Why not just register the file
descriptor and read-handler with Tk?

http://www.faqts.com/knowledge_base/view.phtml/aid/3728

That way the read-handler gets called by Tk's event loop
anytime there's receive data waiting. No extra thread, no
synchronization worries.

If the read-handler takes a _long_ time to complete, it does
make your GUI pause, so that may be a concern.

Unless tk.createfilehandler isn't supported no Wni32
platforms??
 
G

gregarican

Grant said:
Unless tk.createfilehandler isn't supported no Wni32
platforms??

Unfortunately that's the case. As of Python 2.3.4 under Windows XP the
createfilehandler method isn't available. It's only for UNIX as Mac
platforms AFAIK. Shame, as it would be relatively easy to implement.
Instead I'll look to set/check some global flags to hopefully get
things working smoothly. Using Qt it's so much easier. Some apps I used
the QThread class, while others I just set as QTimer instance to run a
regular background process :-(
 
G

gregarican

Eric said:
There's in fact no need to check regularly if there's something in the
Queue: the secondary thread can post a tk custom event to trigger the
treatment automatically from within the main GUI loop. See here:
http://minilien.fr/a0k273

Appreciate the suggestion. This further helps simplify things. So far
so good getting the UDP listener to work well within Tkinter. This
listener takes an incoming CTI screen pop of the caller ID and queries
it against a CRM database to bring up the appropriate contact record if
applicable. One of these days I need to port the CTI library over from
Ruby into Python so I don't have to rely on a UDP socket connection and
can just pass the data natively within Python. But this is a workaround
until that day comes.

Thanks again. I am very impressed with the Python community in terms of
knowledge and helpfulness. It's made matters a lot easier for this
particular Python newbie...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top