当前位置: 首页>>代码示例>>Python>>正文


Python QLocalSocket.bytesAvailable方法代码示例

本文整理汇总了Python中PyQt5.QtNetwork.QLocalSocket.bytesAvailable方法的典型用法代码示例。如果您正苦于以下问题:Python QLocalSocket.bytesAvailable方法的具体用法?Python QLocalSocket.bytesAvailable怎么用?Python QLocalSocket.bytesAvailable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.QtNetwork.QLocalSocket的用法示例。


在下文中一共展示了QLocalSocket.bytesAvailable方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Client

# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import bytesAvailable [as 别名]
class Client(QDialog):
    def __init__(self, parent=None):
        super(Client, self).__init__(parent)

        self.blockSize = 0
        self.currentFortune = None

        hostLabel = QLabel("&Server name:")
        self.hostLineEdit = QLineEdit("fortune")
        hostLabel.setBuddy(self.hostLineEdit)

        self.statusLabel = QLabel("This examples requires that you run the Fortune Server " "example as well.")
        self.statusLabel.setWordWrap(True)

        self.getFortuneButton = QPushButton("Get Fortune")
        self.getFortuneButton.setDefault(True)

        quitButton = QPushButton("Quit")
        buttonBox = QDialogButtonBox()
        buttonBox.addButton(self.getFortuneButton, QDialogButtonBox.ActionRole)
        buttonBox.addButton(quitButton, QDialogButtonBox.RejectRole)

        self.socket = QLocalSocket()

        self.hostLineEdit.textChanged.connect(self.enableGetFortuneButton)
        self.getFortuneButton.clicked.connect(self.requestNewFortune)
        quitButton.clicked.connect(self.close)
        self.socket.readyRead.connect(self.readFortune)
        self.socket.error.connect(self.displayError)

        mainLayout = QGridLayout()
        mainLayout.addWidget(hostLabel, 0, 0)
        mainLayout.addWidget(self.hostLineEdit, 0, 1)
        mainLayout.addWidget(self.statusLabel, 2, 0, 1, 2)
        mainLayout.addWidget(buttonBox, 3, 0, 1, 2)
        self.setLayout(mainLayout)

        self.setWindowTitle("Fortune Client")
        self.hostLineEdit.setFocus()

    def requestNewFortune(self):
        self.getFortuneButton.setEnabled(False)
        self.blockSize = 0
        self.socket.abort()
        self.socket.connectToServer(self.hostLineEdit.text())

    def readFortune(self):
        ins = QDataStream(self.socket)
        ins.setVersion(QDataStream.Qt_4_0)

        if self.blockSize == 0:
            if self.socket.bytesAvailable() < 2:
                return
            self.blockSize = ins.readUInt16()

        if ins.atEnd():
            return

        nextFortune = ins.readQString()
        if nextFortune == self.currentFortune:
            QTimer.singleShot(0, self.requestNewFortune)
            return

        self.currentFortune = nextFortune
        self.statusLabel.setText(self.currentFortune)
        self.getFortuneButton.setEnabled(True)

    def displayError(self, socketError):
        errors = {
            QLocalSocket.ServerNotFoundError: "The host was not found. Please check the host name and port "
            "settings.",
            QLocalSocket.ConnectionRefusedError: "The connection was refused by the peer. Make sure the "
            "fortune server is running, and check that the host name and "
            "port settings are correct.",
            QLocalSocket.PeerClosedError: None,
        }

        msg = errors.get(socketError, "The following error occurred: %s." % self.socket.errorString())
        if msg is not None:
            QMessageBox.information(self, "Fortune Client", msg)

        self.getFortuneButton.setEnabled(True)

    def enableGetFortuneButton(self):
        self.getFortuneButton.setEnabled(self.hostLineEdit.text() != "")
开发者ID:death-finger,项目名称:Scripts,代码行数:87,代码来源:localfortuneclient.py


注:本文中的PyQt5.QtNetwork.QLocalSocket.bytesAvailable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。