我正在尝试制作一个可接收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()
答案 0 :(得分:1)
主线程中不应锁定无限循环,因为它会锁定Qt事件循环,而应在另一个线程中执行并通过信号发送信息
<canvas id="c"></canvas>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r102/three.min.js"></script>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r102/js/CurveExtras.js"></script>