检测 window 在 wnck python 中打开的时间

Detect when a window opens in wnck python

我正在使用 libwnck 制作 window 平铺脚本。我想检测用户何时打开新的 window 以调整其大小。到目前为止,这是我的代码要点:

import gi

gi.require_version("Wnck", "3.0")
from gi.repository import Wnck

screen = Wnck.Screen.get_default()
screen.force_update()

# Here I resize all windows in the current workspace using
# window.set_geometry

# The script ends here however I'd like it to continue forever
# and detect a "window opened event"

从文档来看,似乎有像 do_window_opened 这样的虚拟方法,但我不知道如何让它在 python 中工作。

这是一个有效的简单测试代码。希望能帮到你,是你想要的。

import gi

gi.require_version('Wnck', '3.0')
gi.require_version('Gtk', '3.0')

from gi.repository import Wnck
from gi.repository import Gtk


def test_do_when_window_opened_simple():
    Gtk.init([])
    screen: Wnck.Screen = Wnck.Screen.get_default()
    screen.force_update()

    def do_window_opened(this_screen: Wnck.Screen, opened_window: Wnck.Window):
        print('hello')
        app: Wnck.Application = opened_window.get_application()
        app_name = app.get_name()
        print('app name -> ' + app_name)
        print('window name -> ' + opened_window.get_name())

    screen.connect('window-opened', do_window_opened)

    Gtk.main()


if __name__ == '__main__':
    test_do_when_window_opened_simple()

另请参阅:

Please check the first comment of this link

Please check the example of this link, it's written in C though

注意:Wnck.Screen 对象有一个名为 'do_window_opened' 的函数,但未实现。我在尝试调用 WnckScreen 时收到错误消息“gi.repository.GLib.GError: g-invoke-error-quark: Class WnckScreen 没有实现 window_opened (1)”。