Python 套接字在后台继续运行时PyQt5窗口崩溃

Python 套接字在后台继续运行时PyQt5窗口崩溃,python,api,socket.io,pyqt,pyqt5,Python,Api,Socket.io,Pyqt,Pyqt5,我使用PyQt5和socketIO\u client\u nexus编写了一段python代码。当“s_id”方法使用'socketIO.on'触发时,它将作为客户端启动套接字连接,该客户端将从服务器读取响应 在获得SocketID后,我必须停止socketIO,以防止窗户撞坏。如果我不停止socketIO PyQt5,窗口将自动崩溃 请帮我一个选择,通过它我可以解决这个问题 from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit,

我使用PyQt5socketIO\u client\u nexus编写了一段python代码。当“s_id”方法使用'socketIO.on'触发时,它将作为客户端启动套接字连接,该客户端将从服务器读取响应

在获得SocketID后,我必须停止socketIO,以防止窗户撞坏。如果我不停止socketIO PyQt5,窗口将自动崩溃

请帮我一个选择,通过它我可以解决这个问题

from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit, QApplication
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize, QThread
import requests
import json
import sys
from socketIO_client_nexus import SocketIO, LoggingNamespace
import logging
import requests

class SendInvitation(QMainWindow):
    def __init__(self, arg):

        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(240, 500))
        self.setWindowTitle("GetUserlist")

        self.userid = arg

        url = 'http://192.168.2.36:5200/get_userlist'
        head = {'Content-Type': 'application/x-www-form-urlencoded'}
        data = {'user_id': self.userid}

        ret = requests.post(url, data, head)
        responcedict = json.loads(ret.content)
        print(responcedict)
        userData = responcedict['data']
        x=100
        y=100
        for i in userData:
            useridstring = str(i['user_id'])

            buttonname = QPushButton(useridstring, self)
            buttonname.move(20, y)
            y= y + 30

            buttonname = QPushButton(i['user_name'],self)
            buttonname.move(120, x)
            x = x+30

        self.nameLabel = QLabel(self)
        self.nameLabel.setText('Enter Opponant ID: ')
        self.line1 = QLineEdit(self)
        self.line1.move(120, 25)
        self.line1.resize(100, 25)
        self.nameLabel.move(20, 20)

        OKbutton = QPushButton('OK', self)
        OKbutton.clicked.connect(self.clickMethod)
        OKbutton.resize(100,32)
        OKbutton.move(80, 60)

    def clickMethod(self):
        self.OpponantID = self.line1.text()
        print("Invitation send ID:" + self.OpponantID)
        # return OpponantID

        def getSID(args):
            # print(userid)
            NOWsocketID = args
            print("YOUR SocketID: " + NOWsocketID)
            url = 'http://192.168.2.36:5200/start_game'
            head = {'Content-Type': 'application/x-www-form-urlencoded'}
            data = {'user_id': self.userid,
                    'opponent_id': self.OpponantID,
                    'socket_id': NOWsocketID}
            ret = requests.post(url, data, head)
            print(ret.content)
            socketIO._close()
        socketIO = SocketIO('http://192.168.2.36', 5200, LoggingNamespace)
        socketIO.on('s_id', getSID)
        socketIO.wait()
        SendInvitation.destroy(self)

if __name__ == "__main__":
    chessGui = QApplication(sys.argv)
    UID = 1
    window = SendInvitation(UID)
    window.show()
    sys.exit(chessGui.exec_())

如果将
socketIO.wait()
设置为空,似乎会让您永远等待。(从技术上讲是一个问题?)

尝试将其设置为,
socketIO.wait(秒=2)


我是新来的,如果这不正确,请忽略我。

我在这段代码中添加了线程以解决崩溃窗口问题。现在套接字正在线程上运行,因此GUI窗口不会崩溃

from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit, QApplication
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize, QThread
import requests
import json
import sys
from socketIO_client_nexus import SocketIO, LoggingNamespace
import logging
import requests
import threding

class SendInvitation(QMainWindow):
    def __init__(self, arg):

        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(240, 500))
        self.setWindowTitle("GetUserlist")

        self.userid = arg

        url = 'http://192.168.2.36:5200/get_userlist'
        head = {'Content-Type': 'application/x-www-form-urlencoded'}
        data = {'user_id': self.userid}

        ret = requests.post(url, data, head)
        responcedict = json.loads(ret.content)
        print(responcedict)
        userData = responcedict['data']
        x=100
        y=100
        for i in userData:
            useridstring = str(i['user_id'])

            buttonname = QPushButton(useridstring, self)
            buttonname.move(20, y)
            y= y + 30

            buttonname = QPushButton(i['user_name'],self)
            buttonname.move(120, x)
            x = x+30
        self.nameLabel = QLabel(self)
        self.nameLabel.setText('Enter Opponant ID: ')
        self.line1 = QLineEdit(self)
        self.line1.move(120, 25)
        self.line1.resize(100, 25)
        self.nameLabel.move(20, 20)

        OKbutton = QPushButton('OK', self)
        OKbutton.clicked.connect(self.clickMethod)
        OKbutton.resize(100,32)
        OKbutton.move(80, 60)

    def clickMethod(self):
        self.OpponantID = self.line1.text()
        print("Invitation send ID:" + self.OpponantID)
        # return OpponantID
        thread = threading.Thread(target = self.socket)
        thread.start()

    def socket(self):
        def getSID(args):
            # print(userid)
            NOWsocketID = args
            print("YOUR SocketID: " + NOWsocketID)
            url = 'http://192.168.2.36:5200/start_game'
            head = {'Content-Type': 'application/x-www-form-urlencoded'}
            data = {'user_id': self.userid,
                    'opponent_id': self.OpponantID,
                    'socket_id': NOWsocketID}
            ret = requests.post(url, data, head)
            print(ret.content)
            socketIO._close()
        socketIO = SocketIO('http://192.168.2.36', 5200, LoggingNamespace)
        socketIO.on('s_id', getSID)
        socketIO.wait()
        SendInvitation.destroy(self)

if __name__ == "__main__":
    chessGui = QApplication(sys.argv)
    UID = 1
    window = SendInvitation(UID)
    window.show()
    sys.exit(chessGui.exec_())

QThread不是GUI,为什么要调用show()或setMinimumSize()或setWindowTitle()方法?那是你真正的密码吗?那是错的。现在我已经编辑了代码。实际上,这里有QMainWindow而不是QMainWindowQThread@eyllanesc请再看一次,如果你能给我建议其他的选择,那将是你最大的帮助。是的,你的逻辑是绝对正确的。但我也尝试过添加秒数,就像我添加了socketIO.wait(seconds=2)一样,但通过添加该窗口,窗口不会崩溃,但即使它在2秒钟内没有响应,因为套接字正在等待。线程是执行此任务的一种方法,因为线程能够同时运行GUI和套接字。这可能会解决问题。你认为还有其他办法可以解决这个问题吗?