开发者

Detecting active windows in python with KDE

开发者 https://www.devze.com 2023-02-05 12:00 出处:网络
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 m

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, b开发者_StackOverflowut 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?


One can use the wxPython GUI toolkit for this purpose.

  • It has a method for getting the active window.
  • It has a timer class, althou there are multiple ways you could use.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号