AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 861801
Accepted
Akronix
Akronix
Asked: 2016-12-18 14:47:35 +0800 CST2016-12-18 14:47:35 +0800 CST 2016-12-18 14:47:35 +0800 CST

获取窗口尺寸的工具

  • 772

我需要一个工具来获取任意窗口的宽度和高度。

理想情况下,该工具会减少 Ubuntu 菜单栏的大小。

software-recommendation
  • 5 5 个回答
  • 2354 Views

5 个回答

  • Voted
  1. Byte Commander
    2016-12-18T15:02:30+08:002016-12-18T15:02:30+08:00

    您可以使用wmctrl -lG以下格式在表格中获取所有打开的窗口的列表:

    <window ID> <desktop ID> <x-coordinate> <y-coordinate> <width> <height> <client machine> <window title>
    

    示例输出可能如下所示:

    $ 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
    
    • 11
  2. Akronix
    2016-12-18T15:02:18+08:002016-12-18T15:02:18+08:00

    我xwininfo -all从https://unix.stackexchange.com/questions/14159/how-do-i-find-the-window-dimensions-and-position-accurately-including-decoration找到。

    它确实有效,但我仍然愿意接受更方便的解决方案 => 一个实时 GUI 工具。

    • 9
  3. Best Answer
    Jacob Vlijm
    2016-12-19T00:47:01+08:002016-12-19T00:47:01+08:00

    根据您自己的回答,我了解到您正在寻找一个方便的 GUI 工具,因此:

    用于获取窗口的净大小和实际大小的小型 GUI 工具(动态更新)

    正如下面“解释”中进一步解释的那样,两者都wmctrl返回xdotool一个稍微不正确的窗口大小。

    在此处输入图像描述

    下面的脚本(指标)将显示面板中窗口的“实际”大小和净大小。

    剧本

    #!/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()
    

    如何使用

    1. 该脚本需要安装 xdotool:

      sudo apt-get install xdotool
      
    2. 将脚本复制到一个空文件中,另存为getwindowsize.py

    3. 测试 - 通过以下命令从终端窗口运行脚本:

      python3 /path/to/getwindowsize.py
      
    4. 该脚本选择焦点窗口以动态显示净窗口大小(如 和 的输出)和wmctrl实际窗口大小,包括装饰器等。xdotool

      如果您关闭目标窗口,指示器会显示一条消息:

      在此处输入图像描述

    5. 如果一切正常,请将其添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并添加命令:

      python3 /path/to/getwindowsize.py
      

    解释

    窗口大小,由 wmctrl 和 xdotool 显示

    ...有点不正确

    你提到:

    理想情况下,该工具会减少 Ubuntu 菜单栏的大小

    完整的故事是两者都wmctrl -lG返回xdotool getwindowgeometry没有菜单栏的窗口大小,或者,正如在这个答案中所解释的那样:

    发生的事情是 wmctrl 正在返回装饰内窗口的几何形状(即不包括标题栏和边框)

    如何获得正确的“真实”尺寸

    为了正确获取信息,我们可以运行

    xprop -id <window_id> | grep FRAME
    

    这将输出如下:

    _NET_FRAME_EXTENTS(CARDINAL) = 0, 0, 28, 0
    

    在这里,我们得到我们需要添加到窗口大小的值,作为输出wmctrl和xdotool,到窗口的左侧、右侧、顶部和底部。

    换句话说,在这种情况下,如果 awmctrl显示大小为 200x100,则实际大小为 200x128。

    笔记

    正如 OP 所建议的,用户还可以在指标启动后选择一个窗口,方法是替换:

    window = get(["xdotool", "getactivewindow"])
    

    经过:

    window = get(["xdotool", "selectwindow"])
    

    在脚本中,这些行中的任何一行都可以取消注释。

    • 6
  4. El Guesto
    2016-12-18T19:45:13+08:002016-12-18T19:45:13+08:00

    可以尝试:

    xdotool search --name gnome-panel getwindowgeometry
    

    假设 gnome-panel 是 ubuntu 工具栏的进程名,但谁知道呢。

    (可能需要一个sudo apt-get install xdotool)

    对于一个可能想要进一步改进的即兴 GUI 事物,以便仅显示基本要素:

    zenity --text-info --filename=<(xprop)
    

    它会将指针更改为 xprop 的十字,然后单击窗口,它将在 GTK 对话框中打印 xprop 的信息。

    • 2
  5. Sergiy Kolodyazhnyy
    2016-12-19T04:55:52+08:002016-12-19T04:55:52+08:00

    xwininfo及其优势

    wmctrl最大的问题xdotool是需要安装这些工具 -默认情况下它们不在 Ubuntu 上。但是,Ubuntu 附带xwininfo. 它是一个简单的工具,提供有关用户选择的窗口的信息。

    简单的用法是在终端中输入xwininfo | awk '/Width/||/Height/'(注意awk用于过滤输出),当您的光标更改为x选择您喜欢的任何 GUI 窗口时,它将显示其信息。例如:

    $ xwininfo | awk '/Width/||/Height/'                
      Width: 602
      Height: 398
    

    所以优点是:

    • 很简单
    • 它是默认安装的
    • 它只是文本 - 没什么花哨的,您可以根据需要对其进行过滤和调整

    让 xwininfo 更进一步——显示活动窗口的属性

    当然,如果您像我一样拥有 24/7 全天候开放的终端,这 xwininfo就是您所需要的。一些用户可能更喜欢使用键盘快捷键。下面的脚本(旨在绑定到键盘快捷键)允许您显示一个图形弹出窗口,其中包含有关当前活动窗口的信息。从截图中可以看出,它显示了窗口标题、宽度和高度信息。

    在此处输入图像描述

    在引擎盖下,这并没有做任何特别壮观的事情。它使用来自dbus服务的信息xwininfo并将其放入简单的弹出窗口中。源代码如下。请记住,标准脚本规则适用:确保它具有可执行权限,chmod +x并且在绑定到键盘快捷键时,您将脚本文件的完整路径作为命令提供。

    #!/bin/bash 
    
    get_active_window()
    {
        qdbus org.ayatana.bamf \
              /org/ayatana/bamf/matcher \
              org.ayatana.bamf.matcher.ActiveWindow
    }
    
    get_active_name()
    {
        qdbus org.ayatana.bamf $1 \
              org.ayatana.bamf.view.Name
    }
    
    main()
    {
        active_window=$(get_active_window)
        active_xid=$( awk -F '/' '{print $NF}' <<< "$active_window" )
        echo $active_xid
        active_title=$(get_active_name $active_window)
        dimensions=$(xwininfo -id "$active_xid" | awk '/Width/||/Height/')
        text="$active_title\n""$dimensions"
        zenity --info --text "$text" --width=200 --height=200
    }
    
    main $@
    

    使用 Unity 的顶部面板指示器获取信息。

    在写我的答案时,我意识到这将是一个非常有用的功能,可以合并到我现有的项目之一 - Ayatana 指标中。该指示器允许显示有关 GUI 窗口的全部信息。目前仍在积极开发中。几何信息功能已添加到github 存储库中,并且正在进入我的个人 PPA中。当然,它的使用xwininfo方式虽然略有不同。

    在此处输入图像描述

    • 2

相关问题

  • 有哪些科学绘图软件可用?

  • 最好的思维导图软件是什么?

  • 服务器的最佳rootkit删除工具?

  • 从 Ubuntu 连接到 Windows 的最佳远程桌面工具是什么?[关闭]

  • 是否有 Paint.NET 替代方案?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve