以下是有问题的代码
from PyQt5.QtCore import QThread, pyqtSignal, QByteArray
from PyQt5.QtNetwork import QTcpServer, QHostAddress
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class TcpThread(QThread):
# connected = pyqtSignal(str)
def __init__(self, parent=None):
print("init")
super(TcpThread, self).__init__(parent)
self.timer = QTimer(self)
self.timer.timeout.connect(self.timerFunction)
def connectedSlot(self):
print("服务器建立连接")
self.socket = self.tcpServer.nextPendingConnection()
# self.timer.start(100)
def connectErrorSlot(self):
print("服务器出错")
def timerFunction(self):
print("timer")
if not hasattr(self.timerFunction, 'x'): # hasattr函数的第一个变量为当前函数名,第二个为变量名,加单引号
self.timerFunction.x = 0
a = np.sin(np.pi / 50 * self.timerFunction.x) * 1300
self.timerFunction.x += 1
self.socket.write(a)