'QPixmap' 没有属性 'grabWindow'
'QPixmap' has no attribute 'grabWindow'
我正在尝试将我的代码从 PyQt4 转换为 PyQt5,但出现错误。
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'
您应该改用 QScreen::grabWindow()
。 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.
grabWindow
方法现在可用于 QScreen
class。
您需要创建 QScreen
对象,用 ex 初始化它。 QtGuiApplication.primaryScreen()
然后抢屏
screen.grabWindow(QApplication.desktop().winId())
PyQt5
的完整示例
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QPixmap, QScreen
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QScreen.grabWindow(app.primaryScreen(),
QApplication.desktop().winId()).save(filename, 'png')
我正在尝试将我的代码从 PyQt4 转换为 PyQt5,但出现错误。
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'
您应该改用 QScreen::grabWindow()
。 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.
grabWindow
方法现在可用于 QScreen
class。
您需要创建 QScreen
对象,用 ex 初始化它。 QtGuiApplication.primaryScreen()
然后抢屏
screen.grabWindow(QApplication.desktop().winId())
PyQt5
的完整示例import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QPixmap, QScreen
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QScreen.grabWindow(app.primaryScreen(),
QApplication.desktop().winId()).save(filename, 'png')