http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
Here's an example of how you might do it using Twisted
from twisted.internet import protocol, reactor, tksupport
from twisted.application import service, internet
import Tkinter as Tk
class TkDisplayProtocol(protocol.DatagramProtocol):
def __init__(self, root):
self.root = root
self.frame = Tk.Frame(self.root)
self.frame.pack()
self.label = Tk.Label(self.frame)
self.label.pack()
tksupport.install(self.root)
def datagramReceived(self, data, addr):
self.label.configure(text=repr(data))
application = service.Application('UDP/Tk')
internet.UDPServer(
12345,
TkDisplayProtocol(Tk.Tk())).setServiceParent(application)
Save to "udptk.tac" and run using "twistd -noy udotk.tac".
Jean-Paul
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??
--
Grant Edwards grante Yow! Gee, I feel kind of
at LIGHT in the head now,
visi.com knowing I can't make my
satellite dish PAYMENTS!
> 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 :-(
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965
Thanks. I tried a variation of this Queue posting/Flag checking method
and it worked to a tee. The problem was that my UDP socket query was
blocking things so that thread was hanging everything up. So I used a
socket timeout value along with a try: except: construction to query
the socket for data. Now it's fine.
> gregarican wrote:
>> 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
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
HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
> 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...