3

我希望每次只运行我的应用程序的一个实例。但是当用户第二次尝试打开它时,我希望将第一个窗口带到前面(它可能只是最小化或最小化到任务栏的一角,而用户不知道如何打开它)

我有这段代码可以完成检测工作,它不允许第二个实例。我对必须打开原始窗口的部分有疑问。我已经注释掉了我的一些尝试。

import sys
from PyQt4 import QtGui, QtCore 
import sys

class SingleApplication(QtGui.QApplication):
    def __init__(self, argv, key):
        QtGui.QApplication.__init__(self, argv)
        self._activationWindow=None
        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().toLocal8Bit().data())
    def isRunning(self):
        return self._running

    def activationWindow(self):
        return self._activationWindow

    def setActivationWindow(self, activationWindow):
        self._activationWindow = activationWindow

    def activateWindow(self):
        if not self._activationWindow:
            return
        self._activationWindow.setWindowState(
            self._activationWindow.windowState() & ~QtCore.Qt.WindowMinimized)
        self._activationWindow.raise_()
        self._activationWindow.activateWindow()

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.label = QtGui.QLabel(self)
        self.label.setText("Hello")
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.label)

if __name__ == '__main__':

    key = 'random _ text'

    app = SingleApplication(sys.argv, key)
    if app.isRunning():
        #app.activateWindow()
        sys.exit(1)

    window = Window()  
    #app.setActivationWindow(window)
    #print app.topLevelWidgets()[0].winId()
    window.show()

    sys.exit(app.exec_())
4

2 回答 2

1

我已经使用 win32 api 在 Windows 上完成了这项工作(我不完全确定,但在 macos/unix 上可能有等效的调用)。

将以下导入添加到您的应用程序中,

import win32gui

将窗口标题设置为固定名称(您可以将其 whndl 存储在共享内存中,而不是这样做)

window = Window()  
window.setWindowTitle('Single Application Example')
window.show()

然后将您的 activateWindow 方法更改为如下所示:

def activateWindow(self):
    # needs to look for a named window ...
    whndl = win32gui.FindWindowEx(0, 0, None, "Single Application Example")

    if whndl is 0:
        return #couldn't find the name window ...

    #this requests the window to come to the foreground 
    win32gui.SetForegroundWindow(whndl) 
于 2013-08-19T16:54:01.540 回答
0

您可能对此处提出的解决方案感兴趣

例如,我会尝试:

app = SingleApplication(sys.argv, key)
if app.isRunning():
    window = app.activationWindow()
    window.showNormal()
    window.raise()
    app.activateWindow()
    sys.exit(1)

window = Window()  
app.setActivationWindow(window)
window.setWindowFlags(Popup)
window.show()
于 2013-08-30T03:23:52.827 回答