简体   繁体   中英

Python 3.7 Screenshot from QT5 Widget is black

I'm taking a screenshot of a QtWidget via

import sys
import os
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtGui import QPixmap
from matplotlibwidget import MatplotlibWidget

screenshot = QtWidgets.QWidget.grab(uiCompareGraph.matplotlibwidget_compare) 
screenshot.save("screenshot.png", 'PNG')

which works basically fine when I only take one screenshot. Now my issue is that my program can do a bulk analysis for my data and therefore creates a series of screenshots from which the first one is always black, all of the other one are good. I experimented with time delay before the screenshot but it didn't work. Is there an alternative simple method for creating a screenshot of a QtWidget? Thank you!

Black screen is sometimes a sign that program uses direct hardware rendering, but the case that it's always first one suggests more of like a bug.

How about trying out standalone screenshoting library? There are few for python, for eg https://pypi.org/project/pyscreenshot/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM