使用Pyside时文件描述符错误

时间:2014-12-15 16:46:27

标签: python pyside

我的代码:

class Receiver(QWidget):
def __init__(self):
    self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #Create button
    QToolTip.setFont(QFont('Time New Roman',10))
    super(Example, self).__init__()
...
self.btnConnect.clicked.connect(self.connectserver)
    self.btnConnect.clicked.connect(self.disconnect)
    self.btnLeft.clicked.connect(self.turnleft)
    self.btnRight.clicked.connect(self.turnright)
def connectserver(self):
    self.s.connect((TCP_IP, TCP_PORT))
    length = recvall(conn,16)
    stringData = recvall(s, int(length))
    while True:
        data = numpy.fromstring(stringData, dtype='uint8')
        decimg=cv2.imdecode(data,1)
        cv2.imshow('Client',decimg)
        cv2.waitKey(10)
def disconnect(self):
    self.s.close()
def turnleft(self):
    self.s.send("left")
def turnright(self):
    self.s.send("right")

首先,我点击“连接”按钮。它起了作用,但是当我点击左转或右转时,我收到了一个错误:

Traceback (most recent call last):
  File "D:\NEW\GUI.py", line 69, in turnright
    self.s.send("right")
  File "C:\Python27\lib\socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

1 个答案:

答案 0 :(得分:1)

您已将btnConnect连接到connectserverdisconnect,因此当您点击它时,它会连接,然后立即断开套接字。

如果您尝试将数据发送到已关闭的套接字,则会引发错误的文件描述符错误。