1

I found this question : Get screenshot on Windows with Python?

to take a screenshot of the full desktop:

import sys
from PyQt4.QtGui import QPixmap, QApplication
from datetime import datetime

date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')

However, I would like to take a screenshot of an external window. I have the hwnd of the window I want to take the screenshot off using win32gui.

Community
  • 1
  • 1
edi9999
  • 19,701
  • 13
  • 88
  • 127

1 Answers1

3

According to the documentation for winId, the returned value is platform dependent.

So for Windows, it surely must (famous last words), return a hwnd and thus need no further conversion. If so, then try:

    QPixmap.grabWindow(hwnd).save(filename, 'jpg')

(PS: I have actually tested this now on WinXP, and it works okay for me).

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Hey strangely it worked on one off my computers, but on the other its saying: TypeError: QPixmap.grabWindow(sip.voidptr, int x=0, int y=0, int width=-1, int height=-1): argument 1 has unexpected type 'lo ng' – edi9999 Mar 07 '14 at 20:24
  • on that computer Im on windows 7; but I dont know whats the issue – edi9999 Mar 07 '14 at 20:35
  • its asking me for a sip.voidptr object – edi9999 Mar 07 '14 at 20:43
  • @edi9999. Well, I did say "famous last words" :) The first argument of `grabWindow` should be an `int` (see the [PyQt4 docs](http://pyqt.sourceforge.net/Docs/PyQt4/qpixmap.html#grabWindow)) - so I don't know why things would be different on Windows 7 (and I can't test things myself). What version of Qt and PyQt are you using? – ekhumoro Mar 07 '14 at 21:13
  • I'm currently upgrading both to the latest versions (2.7.6 for python), i will comment again if it still does'nt work – edi9999 Mar 07 '14 at 21:23