Akr*_*nix 15 window software-recommendation layout
我需要一个工具来获取任意窗口的宽度和高度。
理想情况下,该工具会减去 Ubuntu 菜单栏的大小。
Byt*_*der 11
您可以使用wmctrl -lG以下格式获取所有打开窗口的列表:
<window ID> <desktop ID> <x-coordinate> <y-coordinate> <width> <height> <client machine> <window title>
Run Code Online (Sandbox Code Playgroud)
示例输出可能如下所示:
$ wmctrl -lG
0x02a00002 0 -2020 -1180 1920 1080 MyHostName XdndCollectionWindowImp
0x02a00005 0 0 24 61 1056 MyHostName unity-launcher
0x02a00008 0 0 0 1920 24 MyHostName unity-panel
0x02a0000b 0 -1241 -728 1141 628 MyHostName unity-dash
0x02a0000c 0 -420 -300 320 200 MyHostName Hud
0x03a0000a 0 0 0 1920 1080 MyHostName Desktop
0x0400001d 0 61 24 1859 1056 MyHostName application development - A tool to get window dimensions - Ask Ubuntu - Mozilla Firefox
0x04200084 0 61 52 999 745 MyHostName Untitled Document 1 - gedit
Run Code Online (Sandbox Code Playgroud)
我xwininfo -all从https://unix.stackexchange.com/questions/14159/how-do-i-find-the-window-dimensions-and-position-accurately-including-decoration找到。
它确实有效,但我仍然愿意接受更方便的解决方案 => 实时 GUI 工具。
根据您自己的回答,我了解到您正在寻找一个方便的 GUI 工具,因此:
正如在“说明”下面进一步说明,无论是wmctrl和xdotool返回一个稍微不正确windowsize。
下面的脚本(指示器)将显示面板中窗口的“真实”大小和净大小。
#!/usr/bin/env python3
import signal
import gi
gi.require_version('AppIndicator3', '0.1')
gi.require_version('Gtk', '3.0')
import subprocess
from gi.repository import Gtk, AppIndicator3, GObject
import time
from threading import Thread
def get(cmd):
try:
return subprocess.check_output(cmd).decode("utf-8").strip()
except subprocess.CalledProcessError:
pass
# ---
# uncomment either one of two the lines below; the first one will let the user
# pick a window *after* the indicator started, the second one will pick the
# currently active window
# ---
window = get(["xdotool", "selectwindow"])
# window = get(["xdotool", "getactivewindow"])
class Indicator():
def __init__(self):
self.app = 'test123'
iconpath = "unity-display-panel"
self.indicator = AppIndicator3.Indicator.new(
self.app, iconpath,
AppIndicator3.IndicatorCategory.OTHER)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.create_menu())
self.indicator.set_label(" ...Starting up", self.app)
# the thread:
self.update = Thread(target=self.show_seconds)
# daemonize the thread to make the indicator stopable
self.update.setDaemon(True)
self.update.start()
def create_menu(self):
menu = Gtk.Menu()
# separator
menu_sep = Gtk.SeparatorMenuItem()
menu.append(menu_sep)
# quit
item_quit = Gtk.MenuItem('Quit')
item_quit.connect('activate', self.stop)
menu.append(item_quit)
menu.show_all()
return menu
def show_seconds(self):
sizes1 = None
while True:
time.sleep(1)
sizes2 = self.getsize(window)
if sizes2 != sizes1:
GObject.idle_add(
self.indicator.set_label,
sizes2, self.app,
priority=GObject.PRIORITY_DEFAULT
)
sizes1 = sizes2
def getsize(self, window):
try:
nettsize = [int(n) for n in get([
"xdotool", "getwindowgeometry", window
]).splitlines()[-1].split()[-1].split("x")]
except AttributeError:
subprocess.Popen(["notify-send", "Missing data", "window "+window+\
" does not exist\n(terminating)"])
self.stop()
else:
add = [l for l in get(["xprop", "-id", window]).splitlines() if "FRAME" in l][0].split()
add = [int(n.replace(",", "")) for n in add[-4:]]
xadd = add[0]+add[1]; yadd = add[2]+add[3]
totalsize = [str(s) for s in [nettsize[0]+add[0]+add[1], nettsize[1]+add[2]+add[3]]]
displ_sizes = ["x".join(geo) for geo in [[str(s) for s in nettsize], totalsize]]
string = " "+displ_sizes[0]+" / "+displ_sizes[1]
return string+((25-len(string))*" ")
def stop(self, *args):
Gtk.main_quit()
Indicator()
GObject.threads_init()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()
Run Code Online (Sandbox Code Playgroud)
该脚本需要安装 xdotool:
sudo apt-get install xdotool
Run Code Online (Sandbox Code Playgroud)将脚本复制到一个空文件中,另存为 getwindowsize.py
通过以下命令从终端窗口测试运行脚本:
python3 /path/to/getwindowsize.py
Run Code Online (Sandbox Code Playgroud)脚本采聚焦窗口动态地显示净windowsize(如在两者的输出wmctrl和xdotool)与实际的窗口大小,包括装饰等
如果您关闭目标窗口,指示器会显示一条消息:
如果一切正常,请将其添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并添加命令:
python3 /path/to/getwindowsize.py
Run Code Online (Sandbox Code Playgroud)...有点不正确
你提到:
理想情况下,该工具会扣除 Ubuntu 菜单栏的大小
完整的故事是,wmctrl -lG和 都xdotool getwindowgeometry返回没有菜单栏的窗口大小,或者,正如这个答案中所解释的:
发生的事情是 wmctrl 返回装饰内窗口的几何形状(即不包括标题栏和边框)
为了正确获取信息,我们可以运行
xprop -id <window_id> | grep FRAME
Run Code Online (Sandbox Code Playgroud)
这将输出如下:
_NET_FRAME_EXTENTS(CARDINAL) = 0, 0, 28, 0
Run Code Online (Sandbox Code Playgroud)
在这里,我们得到了我们需要添加到该窗口的大小,从输出值wmctrl和xdotool,向左,向右,窗口的顶部和底部。
换句话说,在这种情况下,如果 awmctrl显示大小为 200x100 ,则实际大小为 200x128。
正如 OP 所建议的,用户还可以在指标启动后选择一个窗口,通过替换:
window = get(["xdotool", "getactivewindow"])
Run Code Online (Sandbox Code Playgroud)
经过:
window = get(["xdotool", "selectwindow"])
Run Code Online (Sandbox Code Playgroud)
在脚本中,可以取消注释这些行中的任何一行。