Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to take screenshot of a window in pyqt5 or qt5?

#!/usr/bin/env python3
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QWidget
import sys

app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
widget = QWidget()

screenshot = screen.grabWindow(0, 0, 0, 100, 100)
screenshot.save('shot', 'jpg')

How can i use this to get a window? it only get a part of screen:

screenshot = screen.grabWindow( widget.winId() )

I need a crossplataform method..

like image 349
Siewdass Sf Avatar asked Jul 16 '18 12:07

Siewdass Sf


People also ask

What does PyQt5 stand for?

PyQt is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. PyQt is free software developed by the British firm Riverbank Computing.

What is PyQt5 QtCore?

PyQt5 - Introduction PyQt API is a set of modules containing a large number of classes and functions. While QtCore module contains non-GUI functionality for working with file and directory etc., QtGui module contains all the graphical controls.

What is Qtwidget PyQt5?

The QtWidgets module contains classes that provide a set of UI elements to create classic desktop-style user interfaces.


2 Answers

Ref: http://doc.qt.io/qt-5/qscreen.html#grabWindow

You say you require a screenshot of a window, therefore

screenshot = screen.grabWindow(0, 0, 0, 100, 100)

is not the appropriate call here, since it captures the entire screen, cropped according to the final 4 parameters. (the 100 parameters are width and height).

screenshot = screen.grabWindow( widget.winId() )

captures the widget window. However, the reason you don't perhaps get what you expected on this call is that you don't create a solid widget and/or the widget hasn't been shown. Try the following example, making sure the app is on your primary display before clicking the button.

from PyQt5 import QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()

grab_btn=QtWidgets.QPushButton('Grab Screen')
def click_handler():
    screen = QtWidgets.QApplication.primaryScreen()
    screenshot = screen.grabWindow( w.winId() )
    screenshot.save('shot.jpg', 'jpg')
    w.close()

grab_btn.clicked.connect(click_handler)

layout = QtWidgets.QVBoxLayout()
layout.addWidget(grab_btn)
w.setLayout(layout)
w.show()

sys.exit(app.exec_())

I've tested this on Windows.

like image 103
Jibbity jobby Avatar answered Oct 19 '22 06:10

Jibbity jobby


If any one is coming here and having issues with the other answer, it might be a timing problem.

If you try to grab a screenshot directly during / after initializing the QWidget() instead of at the press of a button it might just make a screenshot of your desktop at the area of your window.

So if you want to grab a screenshot directly after calling __init__, call it after waiting some time with a QTimer (do not use time.sleep() as this would block the GUI).

def initUI(self): 
    (...)       
    QTimer.singleShot(1000, self.saveScreenshot)

def saveScreenshot(self):
    screen = QApplication.primaryScreen()
    screenshot = screen.grabWindow(self.winId() )
    screenshot.save('screenshot.png', 'png')
like image 33
Freya W Avatar answered Oct 19 '22 06:10

Freya W