<legend id='VnIdr'><style id='VnIdr'><dir id='VnIdr'><q id='VnIdr'></q></dir></style></legend>
<tfoot id='VnIdr'></tfoot>
    <i id='VnIdr'><tr id='VnIdr'><dt id='VnIdr'><q id='VnIdr'><span id='VnIdr'><b id='VnIdr'><form id='VnIdr'><ins id='VnIdr'></ins><ul id='VnIdr'></ul><sub id='VnIdr'></sub></form><legend id='VnIdr'></legend><bdo id='VnIdr'><pre id='VnIdr'><center id='VnIdr'></center></pre></bdo></b><th id='VnIdr'></th></span></q></dt></tr></i><div id='VnIdr'><tfoot id='VnIdr'></tfoot><dl id='VnIdr'><fieldset id='VnIdr'></fieldset></dl></div>

        <bdo id='VnIdr'></bdo><ul id='VnIdr'></ul>

      <small id='VnIdr'></small><noframes id='VnIdr'>

        如何编写一个无限循环来接收UDP数据?

        时间:2023-08-05

        <tfoot id='f5Xr8'></tfoot>

        <small id='f5Xr8'></small><noframes id='f5Xr8'>

              <i id='f5Xr8'><tr id='f5Xr8'><dt id='f5Xr8'><q id='f5Xr8'><span id='f5Xr8'><b id='f5Xr8'><form id='f5Xr8'><ins id='f5Xr8'></ins><ul id='f5Xr8'></ul><sub id='f5Xr8'></sub></form><legend id='f5Xr8'></legend><bdo id='f5Xr8'><pre id='f5Xr8'><center id='f5Xr8'></center></pre></bdo></b><th id='f5Xr8'></th></span></q></dt></tr></i><div id='f5Xr8'><tfoot id='f5Xr8'></tfoot><dl id='f5Xr8'><fieldset id='f5Xr8'></fieldset></dl></div>
                <bdo id='f5Xr8'></bdo><ul id='f5Xr8'></ul>

                  <tbody id='f5Xr8'></tbody>

                  <legend id='f5Xr8'><style id='f5Xr8'><dir id='f5Xr8'><q id='f5Xr8'></q></dir></style></legend>
                  本文介绍了如何编写一个无限循环来接收UDP数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

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

                  推荐答案

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

                  You should not have an infinite loop in the main thread since it locks the Qt event loop, instead you should execute it in another thread and send the information through signals

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

                  这篇关于如何编写一个无限循环来接收UDP数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将焦点放在 QlineEdit 小部件上 下一篇:Qt 按钮样式从 ui 预览到原始窗口变化

                  相关文章

                  最新文章

                    <bdo id='EdBPF'></bdo><ul id='EdBPF'></ul>
                1. <tfoot id='EdBPF'></tfoot>

                    1. <small id='EdBPF'></small><noframes id='EdBPF'>

                    2. <i id='EdBPF'><tr id='EdBPF'><dt id='EdBPF'><q id='EdBPF'><span id='EdBPF'><b id='EdBPF'><form id='EdBPF'><ins id='EdBPF'></ins><ul id='EdBPF'></ul><sub id='EdBPF'></sub></form><legend id='EdBPF'></legend><bdo id='EdBPF'><pre id='EdBPF'><center id='EdBPF'></center></pre></bdo></b><th id='EdBPF'></th></span></q></dt></tr></i><div id='EdBPF'><tfoot id='EdBPF'></tfoot><dl id='EdBPF'><fieldset id='EdBPF'></fieldset></dl></div>
                    3. <legend id='EdBPF'><style id='EdBPF'><dir id='EdBPF'><q id='EdBPF'></q></dir></style></legend>