我正在尝试开发一个允许在 gnome 工作区之间移动窗口的 python 应用程序。我正在使用 wnck 与窗口交互,但我想知道如何将窗口从一个工作区移动到另一个工作区。任何人都可以建议我一个方法吗?
我尝试了以下代码
screen = wnck.screen_get_default()
window_list = screen.get_windows()
titlePattern = re.compile('.*Downloads.*')
if len(window_list) == 0:
print "No Windows Found"
for win in window_list:
if(titlePattern.match(win.get_name())):
win.move_to_workspace(screen.get_workspace(2))
但是如果我使用 screen.get_workspace(0) 作为 move_to_workspace 参数,screen.get_workspace(2) 会返回“无”(但它不会移动窗口!)。
我能解决我的问题吗?
提前感谢大家。
请您参考如下方法:
当您调用 screen.get_workspace(2) 时,您的窗口管理器根本没有工作区 2。
您的代码应该适用于实际存在的所有工作区。在 GNOME 3 中,这意味着所有已经有窗口的工作空间加上一个空的工作空间。这意味着 screen.get_workspace(screen.get_workspace_count() - 1) 应该为您提供“空”工作区,如果您正在寻找的话。请注意,没有规范说最后一个工作区是空的,这正是 gnome-shell 当前恰好在做的事情。
您也可以要求窗口管理器增加屏幕上的工作区数量,但没有义务这样做:GNOME 3 窗口管理器不会这样做。
哦,还有关于 Wnck 的一件事要记住:
At its creation, a WnckScreen object will not have fetched information from the X server. If queried immediately after its creation (via wnck_screen_get_windows() or wnck_screen_get_workspaces(), for example), the WnckScreen object will look like there are no workspaces nor windows on the screen. This information is fetched in the main event loop with an idle source, to avoid an expensive synchronous operation on startup. If no main event loop is used, or if the information is needed as soon as possible after the creation of the object, wnck_screen_force_update() can be used to explicitly fetch the information.
我不认为这是你的问题(因为你已经有了一个窗口列表),但我只是仔细检查一下,因为这是一个令人讨厌的陷阱。
