如何才能在特定显示器上最小化窗口?

我有显示器和笔记本电脑的屏幕,并有“显示桌面”function的问题。

笔记本电脑的屏幕显示应始终保持不变的小部件。 主监视器正常使用。

每当我使用键盘快捷键(在我的情况下为Windows键+ d)时,所有窗口都将被隐藏,但我只想隐藏特定显示器上的所有窗口。

这可能吗?

类似于https://superuser.com/questions/200945/how-can-i-make-the-show-desktop-function-only-hide-the-windows-on-a-specific-m的问题,有显着差异我询问不同的操作系统。

仅在一个屏幕上最小化窗口

下面的脚本可用于(仅)最小化左侧屏幕上的窗口仅在右侧屏幕上最小化窗口。

该脚本以12作为参数运行,具体取决于您希望最小化窗口的屏幕。

剧本

 #!/usr/bin/env python3 import subprocess import sys scr = sys.argv[1] def get(cmd): return subprocess.check_output(cmd).decode("utf-8") # find the right side of the left screen edge = [int(s.split("x")[0]) for s in get("xrandr").split() if "+0+" in s][0] # read the window list wlist = [w.split() for w in get(["wmctrl", "-lG"]).splitlines()] for w in wlist: # retrieve the window id and -position w_id = w[0]; xpos = int(w[2]) # check the position of the window, decide action depending on arg. test = xpos < edge if scr == "1" else xpos >= edge if test: # check if the window is "normal" and / or minimized w_data = get(["xprop", "-id", w_id]) if all([not "_NET_WM_STATE_HIDDEN" in w_data, "_NET_WM_WINDOW_TYPE_NORMAL" in w_data]): subprocess.Popen(["xdotool", "windowminimize", w_id]) 

如何使用

  1. 该脚本需要wmctrlxdotool

     sudo apt-get install xdotool wmctrl 
  2. 将脚本复制到空文件中,将其另存为min_screen.py

  3. 要运行它:

     python3 /path/to/min_screen.py 1 

    最小化左侧屏幕上的窗口,和

     python3 /path/to/min_screen.py 2 

    仅在右侧屏幕上最小化窗口

  4. 如果一切正常,请将其添加到快捷键

笔记

  • 该脚本假定安装了python3,但我相信代码不是特定于python3的。
  • 该脚本是在Unity编写的,但与工作区(视口)不同,处理屏幕不应该有所作为。
  • 该脚本不会使用pid 0(例如Idle或其他tkinter窗口)最小化窗口。 如果这是一个问题,请提及。 可以解决。

说明

该脚本首先查找左侧屏幕的右边缘,通过在xrandr的输出中查找+0+的字符串,如下所示:

 1680x1050+0+0 

第一部分, 1680 ,是左侧屏幕的一部分。 随后,我们所要做的就是查看窗口列表( wmctrl -lG )并查看哪个窗口在“下方”或“上方” 1680 ,并相应地采取行动,最小化命令xdotool windowminimize (或不)。

终于“测试”( mmiz ): xprop -id mmiz xprop -id 是检查我们是否正在处理“正常”窗口(而不是例如你的桌面,它也被列为窗口),如果窗口已经是最小化。

另请参阅脚本中的注释。

将脚本绑定到键盘快捷方式

1.在Lubuntu上

要在Lubuntu中编辑全局键盘快捷键,请参阅Lubuntu最小化所有显示桌面键盘快捷键?

在这种情况下:将此答案的脚本保存到计算机上的文件中,使该文件可执行,打开/.config/openbox/lubuntu-rc.xml并替换

     

通过

   /path/to/show_desktop.py 1  

其中/path/to/show_desktop.py (当然)是脚本的路径,目标屏幕是12使脚本可执行

重启计算机以重新加载配置。

2.团结/侏儒

使脚本可执行并选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。 单击“+”并添加命令:

 /path/to/show_desktop.py 1 

…到你选择的捷径

介绍

下面的脚本允许用户单击他们想要的显示,并且该显示器上的所有窗口将被最小化。 此脚本旨在绑定到键盘快捷键,但可以根据需要手动运行。

用法很简单:

  1. 激活脚本,你的鼠标光标将变成十字架
  2. 单击要最小化的显示屏上的任何程序窗口(但不是桌面)。
  3. 该脚本将确定该显示器上的所有窗口,并最小化(图标化)它们。

该程序不需要安装任何其他软件包。 该脚本已在常规Ubuntu 16.04 LTS和Lubuntu 16.04 LTS上进行了测试。 感谢@JourneymanGeek在Fedora 24上使用KDE进行测试!

获取脚本源代码

脚本源代码可以通过在此处手动复制或从我的github存储库中获取来获得。 要通过git获取它,请按照下列步骤操作:

  1. sudo apt-get install git
  2. cd /opt ; sudo git clone https://github.com/SergKolo/sergrep.git
  3. sudo chmod -R +x sergrep

该文件将被称为minimize_display_windows.py 。 确保将其绑定到键盘快捷方式以提供脚本的完整路径。 例如,像这样:

  python /opt/sergrep/minimize_display_windows.py 

源代码

确保将此代码保存到的文件具有可执行权限。

 #!/usr/bin/env python # ########################################################### # Author: Serg Kolo , contact: 1047481448@qq.com # Date: July 3, 2016 # Purpose: Minimize windows on a display which user clicks # Written for: http://askubuntu.com/q/793195/295286 # Tested on: Ubuntu 16.04 LTS,Lubuntu 16.04 Virtual Machine ########################################################### # Copyright: Serg Kolo , 2016 # # Permission to use,copy,modify,and distribute this software is hereby granted # without fee, provided that the copyright notice above and this permission statement # appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # # https://opensource.org/licenses/MIT from gi.repository import GdkX11,Gdk import subprocess def run_sh(cmd): # reusable function to # run shell commands # Returns stdout of the # process proc = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) out = proc.stdout.read().strip() return out # First,let the user click on any window # on the monitor which they want to minimize. # For that we need to extract integer XID of # the window from xwininfo output. # Basically,same as xwininfo -int | awk '/Window id/{print $4}' user_selected = "" for item in run_sh("xwininfo -int").split("\n"): if "Window id" in item: user_selected = item.split()[3] # Here we basically get all the windows on the screen, # and check if their XID matches the one user selected # Once we find that window, we need to know to what display # that window belongs. screen = Gdk.Screen.get_default() for window in screen.get_window_stack(): if str(window.get_xid()) == user_selected: close_screen = int(screen.get_monitor_at_window(window)) # We know which display to close now. Loop over all # windows again, and if they're on the same display # the one that user chose - iconify it ( in X11 terminology # that means minimize the window ) for window in screen.get_window_stack(): if screen.get_monitor_at_window(window) == close_screen : window.iconify() 

演示

可以在我的YouTube频道上找到脚本的简短录音

侧面笔记

最初,我编写了另一个脚本,但它只能在Unity中使用,并且需要xdotool才能存在。 对于那些感兴趣的人,它被发布为gist