1

I'm trying to use PyQt5 to detect when a specific section of the screen changes. My plan is to create an md5 hash of each image and see if they're both the same, but I can't even grab the screen successfully. This is what I have so far:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

win = QApplication.activeWindow()
print(win)

For some reason, it always returns None meaning it can't find an active window. I'm somewhat competent with Python but absolutely new to PyQt5, and I'm stumped. Anyone know what I'm doing wrong?

Alex H
  • 41
  • 3
  • Is that your complete code? If so: you are aware that your code doesn't even start a `QApplication` or even opens any window that could be the `activeWindow`? Hence if you don't open any window - `activeWindow` will return None. So, you should probably first get a working "hello-world" scale PyQt example to actually show a window... – sebastian Feb 26 '15 at 09:21
  • This is my attempt at a "hello-world scale" program to just find an active window. This is the first time I've used PyQt, I thought by 'window' it meant other programs - web browsers, excel, games, whatever. That's usually what 'window' means, especially in the context of 'active window'. Is it something different in PyQt? That's a bit obtuse if so. – Alex H Feb 26 '15 at 15:36
  • I'm not sure whether that obtuse or not - but with `QApplication.activeWindow()` you'll only get the active window that is part of your `QApplication`. You could check http://stackoverflow.com/q/10705712/2319400 if you want to grab "foreign" parts of the screen. – sebastian Feb 26 '15 at 16:03
  • @InfiniteMonkeys. Generally speaking, GUI toolkits like Qt only create the *contents* of windows. The windows themselves (e.g. the frame and titlebar) are ultimately created by the [window-manager](https://en.wikipedia.org/wiki/Window_manager), which is also responsible for positioning the windows, iconifying them, moving them between desktops, etc. So you need to look for some tools which operate at a level above that of GUI toolkits. – ekhumoro Feb 26 '15 at 17:58
  • Googling suggested PyQt which is why I've been trying to make it work. If I can't, I'll do as @ekhumoro suggested, but that link to another question looks like it's the sort of thing I'm after. One thing I don't understand about that snippet is 'app = QApplication(sys.argv)'. As far as I can tell, sys.argv is the list of command line arguments you send the script, which implies that the program running correctly depends on you using the right command line argument. What I can't work out is which argument to use or how to find out. Let me know if I should make a new question for that. Thanks! – Alex H Feb 27 '15 at 00:35
  • @InfiniteMonkeys. You cannot use `QPixmap.grabWindow` to take a screenshot of an external window without knowing its window-id (or `hwnd`, if you're on Windows). Widgets created by your own application know their own window-ids, so that is not a problem. But to get the window-id of an *external* window, you will either have to use a separate tool, or write something yourself (e.g. see [Getting a list of window WIds in QT](http://stackoverflow.com/q/11594184/984421)). – ekhumoro Feb 27 '15 at 01:06
  • In PyQt5, QPixmap.grabWindow doesn't exist anymore, so you'd need to use `QApplication.primaryScreen().grabWindow` instead. E.g. `QApplication.primaryScreen().grabWindow(0)` will return a pixmap of the full desktop, `QApplication.primaryScreen().grabWindow(0, 100, 200, 300, 400)` will return a 300x400 pixmap at the offset +100+200... – mata Feb 27 '15 at 10:22

0 Answers0