Search code examples
pythontkintertwisted

Python: How do I display a UDP message received by a Twisted reactor in a tkinter window?


I am using tkinter and twisted to display UDP messages in a GUI. This is my code:

import tkinter as tk
from twisted.internet import tksupport, reactor
from twisted.internet.protocol import DatagramProtocol

class RX(DatagramProtocol):
    def datagramReceived(self, datagram, address):
        self.msg = datagram.decode('utf-8')
        self.transport.write(datagram, address)

class RX_GUI():
    def __init__(self):
        self.root = tk.Tk()
        tksupport.install(self.root)
        self.reactor = reactor.listenUDP(7201, RX())
        reactor.run()
        self.msg = tk.Label(self.root,text=self.reactor.msg)
        self.msg.grid()
        self.root.mainloop()


RX_GUI()

Here is some companion code to send messages:

import socket
import time
ip = '127.0.0.1'
port = 7201
msg = "Hello World"
while True:
    sock = socket.socket(socket.AF_INET, # Internet
                         socket.SOCK_DGRAM) # UDP
    sock.sendto(msg.encode(), (ip, port))
    time.sleep(1)

How can I get the message, "Hello World" to display to the tkinter window when it is received?


Solution

  • You can pass self.msg to RX() and update it once receiving data:

    class RX(DatagramProtocol):
        def __init__(self, widget):
            super().__init__()
            self.widget = widget
    
        def datagramReceived(self, datagram, address):
            self.msg = datagram.decode('utf-8')
            self.widget['text'] = self.msg  # update the label
            #self.transport.write(datagram, address)  # cause recursive issue
    
    class RX_GUI():
        def __init__(self):
            self.root = tk.Tk()
            self.msg = tk.Label(self.root)
            self.msg.grid()
            self.root.protocol('WM_DELETE_WINDOW', self.quit)
            tksupport.install(self.root)
            self.reactor = reactor.listenUDP(7201, RX(self.msg)) # pass self.msg to RX()
            reactor.run()
            # no need to call self.root.mainloop()
    
        def quit(self):
            reactor.stop()
            self.root.destroy()
    
    RX_GUI()