python脚本最小化除一个窗口之外的所有窗口杀死面板

python脚本最小化除一个窗口之外的所有窗口杀死面板

我是跟着导游走的这里 创建一个键盘快捷键,通过此 python 脚本最小化除当前窗口之外的所有窗口:

#!/usr/bin/env python
import wnck
import gtk

screen = wnck.screen_get_default()

while gtk.events_pending():
    gtk.main_iteration()

windows = screen.get_windows()
active = screen.get_active_window()

for w in windows:
    if not w == active:
            w.minimize()

它可以工作,但它也会杀死我的面板,使我的桌面几乎无法使用。

我应该如何修改脚本,使其在不杀死我的面板的情况下工作?

我使用的是 Ubuntu 16.04.3,Xfce 版本 4.12。

答案1

你没有使用任何 Gtk 的东西。让我们除去那些残骸。

#!/usr/bin/env python
import wnck
screen = wnck.screen_get_default()
windows = screen.get_windows()
active = screen.get_active_window()

for w in windows:
    if not w == active:
            w.minimize()

w == active您可以使用排除窗口以外的条件。检查文档(pydoc wnck不幸的是,它只不过是自动生成的方法列表)或进行一些交互式探索以查看 Windows 上可用的方法:

$ python
Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wnck
>>> screen = wnck.screen_get_default()
>>> active = screen.get_active_window()
>>> dir(active)
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__gdoc__', '__ge__', '__getattribute__', '__gobject_init__', '__grefcount__', '__gt__', '__gtype__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'activate', 'activate_transient', 'chain', 'close', 'connect', 'connect_after', 'connect_object', 'connect_object_after', 'disconnect', 'disconnect_by_func', 'emit', 'emit_stop_by_name', 'freeze_notify', 'get_actions', 'get_application', 'get_class_group', 'get_client_window_geometry', 'get_data', 'get_geometry', 'get_group_leader', 'get_icon', 'get_icon_is_fallback', 'get_icon_name', 'get_mini_icon', 'get_name', 'get_pid', 'get_properties', 'get_property', 'get_screen', 'get_session_id', 'get_session_id_utf8', 'get_sort_order', 'get_state', 'get_transient', 'get_window_type', 'get_workspace', 'get_xid', 'handler_block', 'handler_block_by_func', 'handler_disconnect', 'handler_is_connected', 'handler_unblock', 'handler_unblock_by_func', 'has_icon_name', 'has_name', 'is_above', 'is_active', 'is_below', 'is_fullscreen', 'is_in_viewport', 'is_maximized', 'is_maximized_horizontally', 'is_maximized_vertically', 'is_minimized', 'is_most_recently_activated', 'is_on_workspace', 'is_pinned', 'is_shaded', 'is_skip_pager', 'is_skip_tasklist', 'is_sticky', 'is_visible_on_workspace', 'keyboard_move', 'keyboard_size', 'make_above', 'make_below', 'maximize', 'maximize_horizontally', 'maximize_vertically', 'minimize', 'move_to_workspace', 'needs_attention', 'notify', 'or_transient_needs_attention', 'pin', 'props', 'set_data', 'set_fullscreen', 'set_geometry', 'set_icon_geometry', 'set_properties', 'set_property', 'set_skip_pager', 'set_skip_tasklist', 'set_sort_order', 'set_window_type', 'shade', 'stick', 'stop_emission', 'thaw_notify', 'transient_is_most_recently_activated', 'unmake_above', 'unmake_below', 'unmaximize', 'unmaximize_horizontally', 'unmaximize_vertically', 'unminimize', 'unpin', 'unshade', 'unstick', 'weak_ref']
>>> active.get_name()
'pts/16:  ~   python'
>>> active.is_sticky()
False

每个桌面上都存在粘性窗口。这些通常是桌面环境小部件,应始终显示在屏幕上,因此不应最小化它们。如果这还不够,您可以尝试按名称或其他属性进行匹配。

#!/usr/bin/env python
import wnck
screen = wnck.screen_get_default()
windows = screen.get_windows()
active = screen.get_active_window()

for w in windows:
    if not (w == active or w.is_sticky()):
            w.minimize()

答案2

Ctrl + Alt + d也许不是您正在寻找的答案,但使用窗口管理器快捷方式隐藏所有内容然后使用Alt + Tab?打开最后一个活动视图不是更容易吗?或者甚至更改它们以创建执行两者的命令?

或许可以提供帮助。

如果您的问题是关于语法的...我认为您正在寻找的功能是指南中第二个脚本中的功能

#!/usr/bin/env python
import wnck
import gtk

screen = wnck.screen_get_default()

while gtk.events_pending():
    gtk.main_iteration()

windows = screen.get_windows()
active_app = screen.get_active_window().get_application()

for w in windows:
    if not w.get_application() == active_app:
        w.minimize()

相关内容