Python 如果应用程序已打开,请在再次单击应用程序图标时聚焦打开的应用程序窗口
我对pyqt很陌生。我正在开发这个应用程序。 我做了基本的应用程序 我使用以下代码来防止应用程序多次打开Python 如果应用程序已打开,请在再次单击应用程序图标时聚焦打开的应用程序窗口,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,我对pyqt很陌生。我正在开发这个应用程序。 我做了基本的应用程序 我使用以下代码来防止应用程序多次打开 ''' Class to check weather app is already open or not ''' class SingleApplication(QtWidgets.QApplication): messageAvailable = QtCore.pyqtSignal(object) def __init__(self, argv, key):
''' Class to check weather app is already open or not '''
class SingleApplication(QtWidgets.QApplication):
messageAvailable = QtCore.pyqtSignal(object)
def __init__(self, argv, key):
super().__init__(argv)
# cleanup (only needed for unix)
QtCore.QSharedMemory(key).attach()
self._memory = QtCore.QSharedMemory(self)
self._memory.setKey(key)
if self._memory.attach():
self._running = True
else:
self._running = False
if not self._memory.create(1):
raise RuntimeError(self._memory.errorString())
def isRunning(self):
return self._running
if __name__ == '__main__':
key = common.appTitle
app = SingleApplication(sys.argv,key)
print(app.isRunning())
if app.isRunning():
print("App is already running")
sys.exit(1)
else:
appctxt = ApplicationContext() # 1. Instantiate ApplicationContext
window = Mainwindow()
QApplication.setQuitOnLastWindowClosed(False) ## prevent to close while close message box at the time of background running
mainwindow = window.runwindow()
mainwindow.show()
exit_code = appctxt.app.exec_() # 2. Invoke appctxt.app.exec_()
sys.exit(exit_code)
上述代码可防止多次打开应用程序。如果我们打开多个应用程序,则会显示日志消息“应用程序已在运行”。如果应用程序已处于打开状态,我需要在单击应用程序图标时聚焦或激活已打开的应用程序窗口。请引导我。谢谢最简单的方法是使用和,它允许在应用程序之间创建本地连接和通信,但在这种情况下,只需检查服务器是否已存在或在尝试新连接时收到通知即可:
from PyQt5 import QtCore, QtNetwork, QtWidgets
class UniqueApplication(QtWidgets.QApplication):
anotherInstance = QtCore.pyqtSignal()
def isUnique(self):
socket = QtNetwork.QLocalSocket()
socket.connectToServer('myApp')
return not socket.state()
def startListener(self):
self.listener = QtNetwork.QLocalServer(self)
self.listener.setSocketOptions(self.listener.WorldAccessOption)
self.listener.newConnection.connect(self.anotherInstance)
self.listener.listen('myApp')
print('waiting for connections on "{}"'.format(self.listener.serverName()))
class Test(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout(self)
self.label = QtWidgets.QLabel('Hello, I am new!')
layout.addWidget(self.label)
QtWidgets.QApplication.instance().anotherInstance.connect(self.anotherInstance)
self.count = 0
def anotherInstance(self):
self.count += 1
self.label.setText('That\'s {}, I am still here!'.format(self.count))
self.showNormal()
self.activateWindow()
if __name__ == '__main__':
import sys
app = UniqueApplication(sys.argv)
if not app.isUnique():
print('Application already running!')
else:
app.startListener()
test = Test()
test.show()
sys.exit(app.exec())
谢谢你的回答。我将在我的代码中检查您的概念:)不客气。请注意,在Linux上,服务器名称可能会导致在/tmp上写入套接字文件,因此通常最好使用更复杂的服务器名称,以避免与其他程序等发生冲突。如果已经运行,我需要在单击图标时显示已打开的窗口。如果它处于最小化状态,则需要将注意力集中在您的示例中的屏幕上,以下部分仅打印“应用程序已在运行”,如果不是app.isUnique():如果不是app.isUnique():打印('应用程序已在运行!')@SammuSundar请阅读整个代码,然后再试一次。@musicanmate谢谢你,很抱歉回复得太晚。你的代码运行良好。但在我的例子中,我使用的是FBS appctxt=ApplicationContext()。所以我得到错误“AttributeError:'QApplication'对象没有属性'anotherInstance'”。我正在努力解决这个问题。如果你知道这件事,请告诉我。你的代码在没有FBS的情况下工作正常。谢谢