如何编写接收UDP数据的无限循环?

2024-10-01 22:43:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个应用程序,它接收UDP数据并使用python(PyQt5)在列表视图中显示数据。当我启动接收器时,应用程序卡住了,没有响应。我该怎么解决这个问题?参见下面的代码。在

import sys
import os
import socket
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class udpReceiverApp():
     app = QApplication (sys.argv)
     x = 1
     ip = "192.168.1.4"
     port =515
     server_start = True
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     sock.bind((ip,port))

     def __init__(self):
         self.w = QWidget()
         self.lbl = QLabel ("udp receiver",self.w)
         self.btn = QPushButton ("click me",self.w)
         self.lst = QListWidget(self.w)
         self.lst.resize(200,100)

         self.lbl.move(10, 90)
         self.btn.move(50, 50)
         self.lst.move(10, 90)

         self.btn.clicked.connect(self.startReceiver)

         self.w.setGeometry(300, 300, 300, 200)
         self.w.setWindowTitle("udp receive")
         self.w.show()
         sys.exit(udpReceiverApp.app.exec_())

     def addLstItem(self):
         self.lst.insertItem(0,"item"+str(udpReceiverApp.x) )
         udpReceiverApp.x +=1

     def startReceiver(self):

         while udpReceiverApp.server_start:
               data, addr = self.sock.recvfrom(1024)
               self.lst.insertItem(0,data)

udpReceiverApp()

Tags: 数据fromimportselfapp应用程序movedef
1条回答
网友
1楼 · 发布于 2024-10-01 22:43:11

您不应该在主线程中有一个无限循环,因为它锁定了Qt事件循环,而应该在另一个线程中执行它,并通过信号发送信息

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_())

相关问题 更多 >

    热门问题