Screenshot of a window using python - python

I'm trying to take a screenshot of the curent window using a python script on linux.
I curently have a script which takes a screenshot of the entire screen:
import sys
from PyQt4.QtGui import QPixmap, 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')
But a would like to have only the selected window. I know that the problem comes from grabWindow. But I don't know how to resolve it.

simply replace
QApplication.desktop()
with the widget you want to take the screenshot of.
import sys
from PyQt4.QtGui import *
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
widget = QWidget()
# set up the QWidget...
widget.setLayout(QVBoxLayout())
label = QLabel()
widget.layout().addWidget(label)
def shoot():
p = QPixmap.grabWindow(widget.winId())
p.save(filename, 'jpg')
label.setPixmap(p) # just for fun :)
print "shot taken"
widget.layout().addWidget(QPushButton('take screenshot', clicked=shoot))
widget.show()
app.exec_()

Since Qt5, grabWindow and grabWidget are obsolete (see Obsolete Members for QPixmap)
Instead, you can use QWidget.grab()
p=widget.grab()

Alternatively, instead of
p = QPixmap.grabWindow(widget.winId())
you can also use
p = QPixmap.grabWidget(widget)

PyQt5 update
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')

Related

QGraphicsSVGItem ignores (some) clipping paths. Why?

This SVG image is correctly rendered in Firefox and Inkscape, but for some reason, when using QGraphicsSVGItem without anything fancy, it renders this way:
For reference, this is what it looks like on firefox:
As you can see, the back of the card is not supposed to go beyond the white border.
Am I doing something wrong? Is there a (preferably easy) fix?
MWE:
import sys
from PyQt5 import QtWidgets, QtCore, Qt, QtGui
app = QtWidgets.QApplication(sys.argv)
scene = QtWidgets.QGraphicsScene()
scene.addItem(Qt.QGraphicsSvgItem("back-red.svg"))
graphics_view = QtWidgets.QGraphicsView()
graphics_view.setScene(scene)
graphics_view.show()
sys.exit(app.exec())
Probably your svg does not meet the characteristics that Qt uses (for more information read here). One possible solution is to use QWebEngineView(python -m pip install pyqtwebengine):
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtSvg, QtWebEngineWidgets
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
filename = os.path.join(CURRENT_DIR, "back-red.svg")
scene = QtWidgets.QGraphicsScene()
renderer = QtSvg.QSvgRenderer(filename)
graphics_view = QtWidgets.QGraphicsView()
graphics_view.setScene(scene)
graphics_view.show()
view = QtWebEngineWidgets.QWebEngineView()
view.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
# view.page().setBackgroundColor(QtGui.QColor("transparent"))
view.resize(renderer.viewBox().size())
view.load(QtCore.QUrl.fromLocalFile(filename))
item = scene.addWidget(view)
graphics_view.resize(640, 480)
sys.exit(app.exec())

PyQt5 Changing native dialog save text?

Is it possible to change the Save/Open/Cancel text of native file dialogs called by Qt, e.g.
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
OpenFile = QFileDialog()
OpenFile.getExistingDirectory()
I've tried following some examples in C++, e.g. this, but it doesn't seem to have any effect. Maybe I'm doing something wrong?
Try it:
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
OpenFile = QFileDialog()
#OpenFile.getExistingDirectory()
OpenFile.setFileMode(QFileDialog.DirectoryOnly)
OpenFile.setLabelText(QFileDialog.Accept, "+Accept+")
OpenFile.setLabelText(QFileDialog.Reject, "-REJECT-")
OpenFile.show()
sys.exit(app.exec_())

How to get the current url of QwebView?

i use python and pyqt4 for web view.
here my simple code :
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import os
app = QApplication(sys.argv)
web_view= QWebView()
google='https://www.google.com'
web_view.load(QUrl(google))
web_view.show()
sys.exit(app.exec_())
i want to create a new button where that button any time to execute i want to take current url text link and store it in some variable.
how to do that ?how to take current url from web view ?
QWebView has the url() method that returns the current url, you must call it when you want to get the url.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
w = QWidget()
lay = QVBoxLayout(w)
button = QPushButton("Click Me")
web_view= QWebView()
lay.addWidget(button)
lay.addWidget(web_view)
def foo():
print(web_view.url().toString())
button.clicked.connect(foo)
google='https://www.google.com'
web_view.load(QUrl(google))
w.show()
sys.exit(app.exec_())

snapshoot of widget pyqt5

Hi every one I am traying to get a snapshoot of a widget using pyqt5 I am using this code but I can't create an object of the class QScreen
I get an error :
PyQt5.QtGui.QScreen cannot be instantiated or sub-classed
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap,QScreen
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)
sc=QtGui.QScreen()
sc.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
You can get a reference to the screen with :
sc = app.screens()[0]
This method returns a list of screens, i assume you want the first (with index [0])

'QPixmap' has no attribute 'grabWindow'

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'
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.
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())
Full example for 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')

Categories

Resources