I'm trying to make an app that receives UDP data and shows the data in a list view using python (PyQt5). When I start the receiver, the app gets stuck and does not respond. How can I fix this? See code below.
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()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…