def __init__(self, id, *argv): super(SingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection)
def __init__(self, id): super().__init__() self._id = id # Try to connect to existing application instance's server self._out_socket = QLocalSocket() self._out_socket.connectToServer(self._id) # Connection succeeded? if self._out_socket.waitForConnected(msecs=1000): raise AlreadyRunningError( f"An instance of this application ({id}) is already running.") # Start server to inform future instances that we're already running self._out_socket = None self._server = QLocalServer() self._server.listen(self._id)
class SingleApplication(QApplication): """A QApplication that can tell if another instance of itself is running :param id: A unique string used to identify other instances of the application. :raises AlreadyRunningError: Raised when you try to instantiate a SingleApplication with the ID of an application that is already running on the current machine. """ messageReceived = Signal(str) def __init__(self, id): super().__init__() self._id = id # Try to connect to existing application instance's server self._out_socket = QLocalSocket() self._out_socket.connectToServer(self._id) # Connection succeeded? if self._out_socket.waitForConnected(msecs=1000): raise AlreadyRunningError( f"An instance of this application ({id}) is already running.") # Start server to inform future instances that we're already running self._out_socket = None self._server = QLocalServer() self._server.listen(self._id) @property def is_running(self): """Whether another instance of the application is already running""" return self._is_running @property def id(self): return self._id
def __init__(self, id, *argv): super(QtSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection)
class QSingleApplication(QApplication): onMassageRecived = Signal(str) # Text Massege Event; def init(self, uid: str) -> bool: """ Try To Connect or Create : If Created Then Return True """ self._stream_str: Optional[QTextStream] = None self._server: Optional[QLocalServer] = None self._socket: Optional[QLocalSocket] = None self._uid: str = uid if not self.__connect(): self.__createServer() return True return False def sendTextMassege(self, msg: str): """ Send A Text Massege To Running Instance """ if self._socket and self._stream_str and not self._server: self._stream_str << msg << '\n' self._stream_str.flush() return self._socket.waitForBytesWritten() else: return False def __connect(self): """ Create A Local Socket And Try To Connect """ if self._socket: self._socket.close() socket = QLocalSocket() socket.connectToServer(self._uid) connected = socket.waitForConnected() if not connected: socket.close() return False self._socket = socket self._stream_str = QTextStream(self._socket) self._stream_str.setCodec('UTF-8') return True def __createServer(self): """ Create A Server & Listen as UID """ self._server = QLocalServer() self._server.listen(self._uid) self._server.newConnection.connect(self.__onNewConnection) def __onNewConnection(self): """ On New Socket Connection From Clint """ if self._socket: self._socket.readyRead.disconnect(self.__onReadyRead) self._socket = self._server.nextPendingConnection() if not self._socket: return self._stream_str = QTextStream(self._socket) self._stream_str.setCodec('UTF-8') self._socket.readyRead.connect(self.__onReadyRead) def __onReadyRead(self): """ Stream Ready To Read """ while True: msg = self._stream_str.readLine() if not msg: break self.onMassageRecived.emit(msg)
def __createServer(self): """ Create A Server & Listen as UID """ self._server = QLocalServer() self._server.listen(self._uid) self._server.newConnection.connect(self.__onNewConnection)
# def act(self, reason): # if reason == QSystemTrayIcon.Trigger or reason == QSystemTrayIcon.DoubleClick: # 单击或双击 # window.showNormal() if __name__ == "__main__": app = QApplication(sys.argv) serverName = 'myuniqueservername' lsocket = QLocalSocket() lsocket.connectToServer(serverName) # 如果连接成功,表明server已经存在,当前已有实例在运行 if lsocket.waitForConnected(200) == False: # 没有实例运行,创建服务器 localServer = QLocalServer() localServer.listen(serverName) try: window = MainWindow() window.setWindowTitle("RTT2UART Control Panel V1.3.0") window.show() # window.hide() # mytray = MyTray() # mytray.show() sys.exit(app.exec_()) finally: localServer.close()
class SingleApplication(QApplication): messageReceived = Signal() def __init__(self, id, *argv): super(SingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection) def isRunning(self): return self._isRunning def id(self): return self._id def activationWindow(self): return self._activationWindow def setActivationWindow(self, activationWindow, activateOnMessage=True): self._activationWindow = activationWindow self._activateOnMessage = activateOnMessage def activateWindow(self): if not self._activationWindow: return self._activationWindow.show() self._activationWindow.setWindowState(( self._activationWindow.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive) SetWindowPos( self._activationWindow.winId(), # = always on top. only reliable way to bring it to the front on windows win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # Do not reset topmost; for monitor control this is set... # SetWindowPos(self._activationWindow.winId(), # # disable the always on top, but leave window at its top position # win32con.HWND_NOTOPMOST, # 0, 0, 0, 0, # win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # self._activationWindow.raise_() self._activationWindow.activateWindow() def sendMessage(self, msg): if not self._outStream: return False self._outStream << msg << '\n' self._outStream.flush() return self._outSocket.waitForBytesWritten() def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow() def _onReadyRead(self): while True: msg = self._inStream.readLine() if not msg: break self.messageReceived.emit(msg)
class QtSingleApplication(QApplication): messageReceived = Signal() def __init__(self, id, *argv): super(QtSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection) def isRunning(self): return self._isRunning def id(self): return self._id def activationWindow(self): return self._activationWindow def setActivationWindow(self, activationWindow, activateOnMessage=True): self._activationWindow = activationWindow self._activateOnMessage = activateOnMessage def activateWindow(self): if not self._activationWindow: return self._activationWindow.setWindowState( self._activationWindow.windowState() & ~Qt.WindowMinimized) self._activationWindow.raise_() self._activationWindow.activateWindow() def sendMessage(self, msg): if not self._outStream: return False self._outStream << msg << '\n' self._outStream.flush() return self._outSocket.waitForBytesWritten() def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow() def _onReadyRead(self): while True: msg = self._inStream.readLine() if not msg: break self.messageReceived.emit(msg)
class QtSingleApplication(QApplication): messageReceived = Signal(str) def __init__(self, id, *argv): super(QtSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection) def isRunning(self): return self._isRunning def id(self): return self._id def activationWindow(self): return self._activationWindow def setActivationWindow(self, activationWindow, activateOnMessage=True): self._activationWindow = activationWindow self._activateOnMessage = activateOnMessage def activateWindow(self): if not self._activationWindow: return self._activationWindow.setWindowState( self._activationWindow.windowState() & ~Qt.WindowMinimized) self._activationWindow.raise_() self._activationWindow.activateWindow() def sendMessage(self, msg): if not self._outStream: return False self._outStream << msg << '\n' self._outStream.flush() return self._outSocket.waitForBytesWritten() def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow() def _onReadyRead(self): while True: msg = self._inStream.readLine() if not msg: break self.messageReceived.emit(msg)