Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt5 udp example

import sys
import os
import socket
from PyQt5 import QtCore, QtWidgets

class UDPWorker(QtCore.QObject):
    dataChanged = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(UDPWorker, self).__init__(parent)
        self.server_start = False

    @QtCore.pyqtSlot()
    def start(self):
        self.server_start = True
        ip = "192.168.1.4"
        port = 515
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.bind((ip,port))
        self.process()

    def process(self):
        while self.server_start:
            data, addr = self.sock.recvfrom(1024)
            self.dataChanged.emit(str(data))

class UDPWidget(QtWidgets.QWidget):
    started = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(UDPWidget, self).__init__(parent)
        btn = QtWidgets.QPushButton("Click Me")
        btn.clicked.connect(self.started)
        self.lst = QtWidgets.QListWidget()

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(QtWidgets.QLabel("udp receiver"))
        lay.addWidget(btn)
        lay.addWidget(self.lst)

        self.setWindowTitle("udp receive")

    @QtCore.pyqtSlot(str)
    def addItem(self, text):
        self.lst.insertItem(0, text)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = UDPWidget()
    worker = UDPWorker()
    thread = QtCore.QThread()
    thread.start()
    worker.moveToThread(thread)
    w.started.connect(worker.start)
    worker.dataChanged.connect(w.addItem)
    w.show()
    sys.exit(app.exec_())
Comment

PREVIOUS NEXT
Code Example
Python :: pandas return indices that match 
Python :: l1=[122, 5, 9, 4] l2=[991, 4, 8, 3] x=[l1[i]-l2[i] for i in range(abs(len(l1)), abs(len(l2)))] print (x) 
Python :: hexing floats 
Python :: convert float to booelan 
Python :: filter numbers with bounds filter_bounds python 
Python :: stop animation matplotlib 
Python :: torch.tensor.expand 
Python :: Freqtrade - Informative Pairs 
Python :: how to add sum of range in python 
Python :: python get next item from generator 
Python :: pvector python processing 
Python :: shotgun meanign 
Python :: fine tune huggingface model pytorch 
Python :: qmenu hide python 
Python :: insert in a sorted list python 
Python :: python text to speech arabic 
Python :: python file browser 
Python :: python 1.0 
Python :: python merge sort 
Python :: signup view django 
Python :: python bytes to hex 
Python :: how to update a python package 
Python :: python oneline if 
Python :: rotate 2 dimensional list python 
Python :: gfg placement 
Python :: add new column to pandas dataframe 
Python :: global python 
Python :: how to refresh page in flask 
Python :: socket.accept python 
Python :: count python 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =