Skip to content
Advertisement

How to grab a desktop screenshot with PyQt?

Can I take a screenshot from desktop or any window with PyQt? How to handle keyPressEvent on desktop?

Thanks.

Advertisement

Answer

Example of how to take a screenshot of the desktop:

import sys
from PyQt4.QtGui import QPixmap, QApplication

app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save('screenshot.jpg', 'jpg')

If you want to take a screenshot of a specific window, replace QApplication.desktop() with the widget you want to take a screenshot of.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement