Tkinter listener thread?

Jean-Paul Calderone exarkun at divmod.com
Thu Jan 26 12:39:25 EST 2006


On 26 Jan 2006 08:46:11 -0800, gregarican <greg.kujawa at gmail.com> 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 :-)

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



More information about the Python-list mailing list