我有一个位于托盘中的应用程序,当您单击托盘菜单中的某个菜单项时,我试图让它显示/隐藏。该代码几乎可以正常工作,但该窗口显示在其他窗口的后面,而不是像它应该的那样位于顶部。 (注意,我不希望它总是在顶部,只是弹出到窗口堆栈的顶部。)
它似乎在 Windows 上工作得很好,但在 Mac 上,它始终低于同时拉到它前面的任何其他窗口。
相关代码如下。
def on_hide_frame(self, event):
self.frame.on_iconify(event)
def on_restore_frame(self, event):
if self.frame.IsIconized():
self.frame.Iconize(False)
if not self.frame.IsShown():
self.frame.Show(True)
self.frame.Raise()
另请注意,调用此方法时,self.frame
与 app.GetTopWindow()
是同一个对象,将两者互换并不能修复错误。
最佳答案
似乎是因为该应用程序是后台应用程序,因此需要一个激活事件。这可能不是最好的方法,但使用 Applescript 向自己发送激活事件相当容易:
subprocess.Popen(['osascript', '-e', '''\
tell application "System Events"
set procName to name of first process whose unix id is %s
end tell
tell application procName to activate
''' % os.getpid()])
关于macos - 在 wxPython for Mac 中获取顶部窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901067/