我是 PyQt 新手。我一直在寻找如何找到当前打开的 PyQt 应用程序的窗口并将其置于前面。到目前为止,我发现的只是一个使用 pywin32 的示例(因此特定于 Windows)。我想问是否有一种独立于平台的方法可以实现目标。任何帮助将不胜感激。
这是我的代码。 activateWindow() 函数应该将其带到前面。
class TestApp(QtGui.QApplication):
def __init__(self, argv, key):
QtGui.QApplication.__init__(self, argv)
self._activationWindow=None
self._memory = QtCore.QSharedMemory()
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 | QtCore.Qt.WindowActive)
self._activationWindow.raise_()
self._activationWindow.show()
self._activationWindow.activateWindow()
最佳答案
一个完整的、独立于平台的解决方案可能是遥不可及的。 Qt 支持的每个平台都有不同的行为方式,activateWindow
似乎有些问题。
首先,这是 Qt 文档中关于 activateWindow
的说法:
This function performs the same operation as clicking the mouse on the title bar of a top-level window. On X11, the result depends on the Window Manager. If you want to ensure that the window is stacked on top as well you should also call raise(). Note that the window must be visible, otherwise activateWindow() has no effect.
和:
On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.
有关困难的更多确认证据,请查看 Qt 论坛上的这些帖子:
关于python - 如何找到事件的 PyQt 窗口并将其置于最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22815608/