'QPixmap' has no attribute 'grabWindow

2019-08-13 02:35发布

问题:

I am trying convert my code from PyQt4 to PyQt5 but I am getting errors.

import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import 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')


Traceback (most recent call last):
  File "C:\Python34\Projects\name.py", line 9, in <module>
    QPixmap.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
AttributeError: type object 'QPixmap' has no attribute 'grabWindow'

回答1:

You should use QScreen::grabWindow() instead. QPixmap::grabWindow() is deprecated in Qt 5.0 because:

there might be platform plugins in which window system identifiers (WId) are local to a screen.



回答2:

grabWindow method is now available in QScreen class.

You need to create QScreen object, initialize it with ex. QtGuiApplication.primaryScreen() and then grab the screen

screen.grabWindow(QApplication.desktop().winId())


标签: python pyqt5