我有两个窗口A和B。是否可以将两个窗口链接在一起,从而切换到A也会引发B,或者切换到B也会引发A?
我知道使用多个工作空间是一种替代选择,但我想知道是否还可以?
以下脚本允许选择两个窗口,并且当两个窗口都打开时,当用户将其中一个聚焦时,它将同时升高两个窗口。例如,如果一个链接寡妇A和B,则对A或B进行巫婆会使两者都高于其他寡妇。
要停止脚本,您可以killall link_windows.py
在terminal中使用,或关闭并重新打开其中一个窗口。您也可以通过X在两个窗口选择弹出对话框中按关闭按钮来取消执行。
潜在的调整:
运行脚本为:
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窗口,按OK或Enter。鼠标指针将变成十字形。单击要链接的窗口之一。
出现第二个弹出窗口,要求您选择窗口#2,按OK或Enter。再次,鼠标指针将变成十字形。单击要链接的另一个窗口。之后,将开始执行。
每当您将一个窗口聚焦时,脚本都会将另一个窗口上移,但会将焦点返回到最初选择的那个窗口(请注意-延迟四分之一秒以获得最佳性能),从而产生将窗口链接在一起的感觉。
如果两次都选择同一窗口,则脚本将退出。如果您随时单击弹出对话框的关闭按钮,则脚本将退出。
也可以作为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()
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
这些可以忽略。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句