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

时间:2018-06-14 07:31:55

标签: python api socket.io pyqt pyqt5

我使用 PyQt5 " socketIO_client_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_())

2 个答案:

答案 0 :(得分:1)

这里我在这段代码中添加线程来解决崩溃窗口问题。现在socket在线程上运行,因此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_())

答案 1 :(得分:0)

如果您将socketIO.wait()设置为空,则会让您永远等待。 (从技术上来说是悬挂?)

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

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