我对pyqt很陌生。我正在开发这个应用程序。 我做了基本的应用程序 我使用以下代码来防止应用程序多次打开
''' 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)
上述代码可防止多次打开应用程序。如果我们打开多个应用程序,则会显示日志消息“应用程序已在运行”。如果应用程序已处于打开状态,我需要在单击应用程序图标时聚焦或激活已打开的应用程序窗口。请引导我。多谢各位
最简单的方法是使用QLocalServer和QLocalSocket,这允许创建本地连接并在应用程序之间通信,但在这种情况下,只需检查服务器是否已存在或在尝试新连接时收到通知即可:
相关问题 更多 >
编程相关推荐