使用 KDE 检测 python 中的活动窗口

发布于 2024-10-12 23:47:25 字数 878 浏览 5 评论 0原文

我正在编写一个小的 python 脚本来跟踪我如何在程序之间分配时间。它需要检测当前活动的窗口(理想情况下是控制该窗口的应用程序,但我可以只使用窗口标题)以及空闲时间。

目前,我正在使用 KIdleTime 来确定多长时间我没有碰过我的电脑,它本身运行得很好。

我还尝试使用 wnck 来确定哪些窗口处于活动状态,同样,这有效本身就很好。

当我尝试将两者合并为一个程序时,我遇到了麻烦。由于 wnck 是一个 gnome 库,而 KIdleTime 是一个 KDE 库,因此它们需要在自己的线程上运行,以便触发窗口更改和空闲超时信号。

我在 python 中找不到一种方法来执行此操作 - 如果我使用线程模块尝试在单独的线程上运行其中一个,KDE 库会抱怨它不在主线程上,或者 Gnome 库有一个令人印象深刻的崩溃并吃掉它可以获得的所有内存(顺便说一句,ulimit 在这种情况下非常有用,我可以推荐它)。

所以,我有四个选择:

  1. 找到一个可以检测的 KDE 库 当窗口改变时 - 我会 曾以为 dbus 会暴露 这在 kwin 中,但似乎没有 到。
  2. 找到一种同时运行 kde 和 gnome 库的方法 时间。
  3. 查找在 Gnome 事件线程上运行的 KIdleTime 的等效项。
  4. 使用计时器从 Gnome 线程内轮询 KIdleTime(即 我现在正在做的事情,但远非理想,它导致不精确 确定空闲时间)。

谁能为我提供解决方案?

I'm writing a small python script that tracks how I'm dividing my time between programs. It needs to detect the currently active window (ideally, the application controlling that window, but I can make do with just the window title), as well as idle time.

At the moment, I'm using KIdleTime to determine how long I've been not touching my computer, which works fine by itself.

I also try to use wnck to determine which windows are active, and again, this works fine by itself.

It's when I try to combine the two into one program that I run into troubles. Since wnck is a gnome library and KIdleTime is a KDE library, they need to be run on their own threads for the window change and idle timeout signals to fire.

I'm having trouble finding a way to do this in python - if I use the threading module to try to run one of them on a separate thread, the KDE library complains that it's not on the main thread, or the Gnome library has an impressive meltdown and eats all the memory it can get (incidentally, ulimit was very useful in this case, I can recommend it).

So, I have four options:

  1. Find a KDE library that can detect
    when the window is changed - I would
    have thought that dbus would expose
    this in kwin, but it doesn't seem
    to.
  2. Find a way of running both the kde and gnome libraries at the same
    time.
  3. Find an equivalent of KIdleTime that runs on the Gnome event thread.
  4. Use timers to poll KIdleTime from within the Gnome thread (which is
    what I'm doing now, but is far from ideal, it leads to imprecision in
    determining idle times).

Can anyone offer me a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2024-10-19 23:47:25

为此,可以使用 wxPython GUI 工具包

当然,您可以忘记 QT、Gnome 和所有其他图形库。
以下是 wx 可以执行的操作

One can use the wxPython GUI toolkit for this purpose.

Of course, you can forget about QT, Gnome, and all other graphical libraries.
Here is what wx can do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文