如何将要组合在一起的窗户分组?

唐Tong

我有两个窗口A和B。是否可以将两个窗口链接在一起,从而切换到A也会引发B,或者切换到B也会引发A?

我知道使用多个工作空间是一种替代选择,但我想知道是否还可以?

塞尔吉(Sergiy Kolodyazhnyy)

介绍

以下脚本允许选择两个窗口,并且当两个窗口都打开时,当用户将其中一个聚焦时,它将同时升高两个窗口。例如,如果一个链接寡妇A和B,则对A或B进行巫婆会使两者都高于其他寡妇。

要停止脚本,您可以killall link_windows.py在terminal中使用,或关闭并重新打开其中一个窗口。您也可以通过X在两个窗口选择弹出对话框中按关闭按钮来取消执行

潜在的调整:

  • 脚本的多个实例可用于对两个窗口进行分组。例如,如果我们有窗口A,B,C和D,我们可以将A和B链接在一起,并将C和D链接在一起。
  • 多个窗口可以分组在一个窗口下。例如,如果我将窗口B链接到A,将C链接到A,将D链接到A,这意味着如果我始终切换到A,则可以同时引发所有4个窗口。

用法

运行脚本为:

python link_windows.py

该脚本与Python 3兼容,因此也可以作为

python3 link_windows.py

有两个命令行选项:

  • --quiet-q,可以使GUI窗口静音。使用此选项,您只需在任意两个窗口上单击鼠标,脚本就会开始链接这些窗口。
  • --help-h,打印用法和描述信息。

-h选件将产生以下信息:

$ python3 link_windows.py  -h                                                                                            
usage: link_windows.py [-h] [--quiet]

Linker for two X11 windows.Allows raising two user selected windows together

optional arguments:
  -h, --help  show this help message and exit
  -q, --quiet  Blocks GUI dialogs.

可以通过查看其他技术信息pydoc ./link_windows.py,该信息./表示您必须与脚本位于同一目录中。

两个窗口的简单使用过程:

  1. 将出现一个弹出窗口,要求您选择#1窗口,按OKEnter鼠标指针将变成十字形。单击要链接的窗口之一。

  2. 出现第二个弹出窗口,要求您选择窗口#2,按OKEnter再次,鼠标指针将变成十字形。单击要链接的另一个窗口。之后,将开始执行。

  3. 每当您将一个窗口聚焦时,脚本都会将另一个窗口上移,但会将焦点返回到最初选择的那个窗口(请注意-延迟四分之一秒以获得最佳性能),从而产生将窗口链接在一起的感觉。

如果两次都选择同一窗口,则脚本将退出。如果您随时单击弹出对话框的关闭按钮,则脚本将退出。

脚本源

也可以作为GitHub Gist获得

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: Sergiy Kolodyazhnyy
Date:  August 2nd, 2016
Written for: https://askubuntu.com/q/805515/295286
Tested on Ubuntu 16.04 LTS
"""
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk, Gtk
import time
import subprocess
import sys
import argparse


def run_cmd(cmdlist):
    """ Reusable function for running shell commands"""
    try:
        stdout = subprocess.check_output(cmdlist)
    except subprocess.CalledProcessError:
        sys.exit(1)
    else:
        if stdout:
            return stdout


def focus_windows_in_order(first, second, scr):
    """Raise two user-defined windows above others.
       Takes two XID integers and screen object.
       Window with first XID will have the focus"""

    first_obj = None
    second_obj = None

    for window in scr.get_window_stack():
        if window.get_xid() == first:
            first_obj = window
        if window.get_xid() == second:
            second_obj = window

    # When this  function is called first_obj is alread
    # raised. Therefore we must raise second one, and switch
    # back to first
    second_obj.focus(int(time.time()))
    second_obj.get_update_area()
    # time.sleep(0.25)
    first_obj.focus(int(time.time()))
    first_obj.get_update_area()


def get_user_window():
    """Select two windows via mouse. Returns integer value of window's id"""
    window_id = None
    while not window_id:
        for line in run_cmd(['xwininfo', '-int']).decode().split('\n'):
            if 'Window id:' in line:
                window_id = line.split()[3]
    return int(window_id)


def main():
    """ Main function. This is where polling for window stack is done"""

    # Parse command line arguments
    arg_parser = argparse.ArgumentParser(
        description="""Linker for two X11 windows.Allows raising """ +
                    """two user selected windows together""")
    arg_parser.add_argument(
                '-q','--quiet', action='store_true',
                help='Blocks GUI dialogs.',
                required=False)
    args = arg_parser.parse_args()

    # Obtain list of two user windows
    user_windows = [None, None]
    if not args.quiet:
        run_cmd(['zenity', '--info', '--text="select first window"'])
    user_windows[0] = get_user_window()
    if not args.quiet:
        run_cmd(['zenity', '--info', '--text="select second window"'])
    user_windows[1] = get_user_window()

    if user_windows[0] == user_windows[1]:
        run_cmd(
            ['zenity', '--error', '--text="Same window selected. Exiting"'])
        sys.exit(1)

    screen = Gdk.Screen.get_default()
    flag = False

    # begin watching for changes in window stack
    while True:

        window_stack = [window.get_xid()
                        for window in screen.get_window_stack()]

        if user_windows[0] in window_stack and user_windows[1] in window_stack:

            active_xid = screen.get_active_window().get_xid()
            if active_xid not in user_windows:
                flag = True

            if flag and active_xid == user_windows[0]:
                focus_windows_in_order(
                    user_windows[0], user_windows[1], screen)
                flag = False

            elif flag and active_xid == user_windows[1]:
                focus_windows_in_order(
                    user_windows[1], user_windows[0], screen)
                flag = False

        else:
            break

        time.sleep(0.15)


if __name__ == "__main__":
    main()

笔记:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将向量与列表中的公共成分组合在一起?

如何在SAS中将组合在一起的观察分组

语义ui构建如何将此项组合在一起

如何将Scala中的每个n元素组合在一起?

如何将多个稀疏矩阵和密集矩阵组合在一起

如何将多个折线图组合在一起

如何将2个折线图组合在一起

如何将所有单词组合在一起?

如何将 Python 中的值组合在一起?

在Python中,如何将多个轴组合在一起?

如何将多个OpenAPI 3规范文件组合在一起?

如何将2个脚本组合在一起?

如何将对象的 JSON 数组组合在一起

Three.js-如何将多个导入的模型组合在一起

如何将 2 个选择元素组合在一起 html

如何将多个列组合在一起并绘制条形图?

Groovy / Grails:如何将多个键组合在一起?

如何将两个通用队列组合在一起?

如何将分组的输入与对齐的表单结合在一起?

如何将基于键的行与组合在单列中的值组合在一起

如何将关键代码组合在一起以在javascript中一起工作

如何将排序依据和分组依据组合在一起?条件:按一列排序,按另一列排序

使用PHP将CSV中数组的不同部分组合在一起?

如何将相似的类组合在一起或使其成为一个函数?

如何将列与 Pandas 中的虚拟变量组合在一起(一个输出)?

如何将警报中的textinput字段与R中的模态对话框组合在一起?

在Stata中,如何将多个具有不同轴的coefplot组合在一起?

如何将Fn :: FindInMap中的列表与其他项目组合在一起?

熊猫如何将具有复杂规则/条件的两行组合在一起