2012-02-01 59 views
7

如何檢查pygtk窗口在當前桌面上?檢查窗口是否在當前桌面上?

我固定的程序有:

if self.pymp.window.get_property('visible'): 
    self.pymp.window.hide() 
else: 
    self.pymp.window.move(self.pymp.position[0], self.pymp.position[1]) 
    self.pymp.window.show() 
    self.pymp.window.present() 

我想替換:

if self.pymp.window.get_property('visible'): 

有了:

if self.pymp.window.get_property('visible') and window_is_on_current_workspace(self.pymp.window): 

我可以wnck實現window_is_on_current_workspace(window)爲:

def window_is_on_current_workspace(window): 
    import wnck 
    v = wnck.screen_get_default() # needed due to known bug 
    xid = window.window.xid 
    win = None 
    while win is None: 
     win = wnck.window_get(xid) 
     if gtk.events_pending(): 
      gtk.main_iteration() 
    wor = win.get_screen().get_active_workspace() 
    return win.is_on_workspace(wor) 

它的作品,但它是惡作劇。有一個更好的方法嗎?

回答

2

您可以使用wnck。這裏是一個示例,

from gi.repository import Wnck # import wnck for gtk2 

screen = Wnck.Screen.get_default() 

def window_is_on_current_workspace(window): 
    for win in screen.get_windows(): 
     if win.get_pid() == os.getpid(): # You could use other methods to determine if you've the right window, like window.name; Just make sure you can uniquely identify it 
      return win.get_workspace() == s.get_active_workspace() 

     # This works if you have only one window per process, it's just an example. You can use any method to match your gtk window with the x11 window returned by wnck.Screen 
+0

整個循環可以被'window.is_on_workspace(screen.get_active_workspace())'替代。無需遍歷所有窗口。 – 2014-08-03 15:31:01

相關問題