pyqt4 - 单个应用程序 - 调出原始窗口以尝试第二次打开应用程序

时间:2013-08-18 03:54:13

标签: qt pyqt pyqt4

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

我有这个代码执行检测作业,它不允许第二个实例。我有打开原始窗口的部分有问题。我已经评论了我的一些尝试。

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_())

2 个答案:

答案 0 :(得分: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) 

答案 1 :(得分:0)

您可能会对提出的解决方案感兴趣here

例如,我会尝试:

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()