Tkinter listener thread?

53 views
Skip to first unread message

gregarican

unread,
Jan 26, 2006, 11:46:11 AM1/26/06
to
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 :-)

Steve Holden

unread,
Jan 26, 2006, 12:32:32 PM1/26/06
to pytho...@python.org

Jean-Paul Calderone

unread,
Jan 26, 2006, 12:39:25 PM1/26/06
to pytho...@python.org

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

Grant Edwards

unread,
Jan 26, 2006, 12:46:21 PM1/26/06
to
On 2006-01-26, Steve Holden <st...@holdenweb.com> wrote:
> 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

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!

gregarican

unread,
Jan 26, 2006, 1:08:08 PM1/26/06
to
Grant Edwards wrote:

> 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 :-(

gregarican

unread,
Jan 26, 2006, 3:08:44 PM1/26/06
to
Steve Holden wrote:

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

Eric Brunel

unread,
Jan 27, 2006, 4:03:07 AM1/27/06
to
On Thu, 26 Jan 2006 17:32:32 +0000, Steve Holden <st...@holdenweb.com>
wrote:

> 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+-'])"

gregarican

unread,
Jan 27, 2006, 10:59:36 AM1/27/06
to
Eric Brunel wrote:

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

Reply all
Reply to author
Forward
0 new messages