def save(self, fname, format, draft): if is_text_string(fname): if format == "pdf": self.app = guidata.qapplication() if draft: mode = QPrinter.ScreenResolution else: mode = QPrinter.HighResolution printer = QPrinter(mode) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOrientation(QPrinter.Landscape) printer.setOutputFileName(fname) printer.setCreator("guiqwt.pyplot") self.print_(printer) else: if self.win is None: self.show() if PYQT5: pixmap = self.win.centralWidget().grab() else: pixmap = QPixmap.grabWidget(self.win.centralWidget()) pixmap.save(fname, format.upper()) else: # Buffer fd = fname assert hasattr(fd, "write"), "object is not file-like as expected" if self.win is None: self.show() pixmap = QPixmap.grabWidget(self.win.centralWidget()) buff = QBuffer() buff.open(QIODevice.ReadWrite) pixmap.save(buff, format.upper()) fd.write(buff.data()) buff.close() fd.seek(0)
def copy_to_clipboard(self): """Copy widget's window to clipboard""" clipboard = QApplication.clipboard() if PYQT5: pixmap = self.grab() else: pixmap = QPixmap.grabWidget(self) clipboard.setPixmap(pixmap)
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: if PYQT5: pm = self.grab() else: pm = QPixmap.grabWidget(self) color = QColor() color.setRgb(pm.toImage().pixel(event.x(), event.y())) self.colorSelected.emit(color) event.accept()
def take_screenshot(widget): """Take screenshot and save it to the data folder""" if PYQT5: pixmap = widget.grab() else: pixmap = QPixmap.grabWidget(widget) bname = (widget.objectName().lower() + ".png").replace("window", "") bname = bname.replace("plot", "").replace("widget", "") pixmap.save(osp.join(TEST_PATH, "data", bname)) QTimer.singleShot(0, QApplication.instance().quit)
def copy(fig=None): if fig is None: canvas = plt.gcf().canvas else: canvas = fig.canvas canvas.draw() try: pixmap = QPixmap.grabWidget(canvas) except: # PyQt5+ pixmap = canvas.grab() QApplication.clipboard().setPixmap(pixmap) print("Image copied to clipboard.")
def take_picture(widget: QWidget, filename: str) -> str: """ Takes a screenshot and saves it to the filename given, ensuring the call is processed through a slot if the call is from a separate thread """ if hasattr(widget, 'grab'): pix = widget.grab() else: pix = QPixmap.grabWidget(widget) success = pix.save(filename) if not success: raise RuntimeError(f"Error saving widget image to file '{filename}'")
def save_widget(self, fname): """Grab widget's window and save it to filename (\*.png, \*.pdf)""" fname = to_text_string(fname) if fname.lower().endswith(".pdf"): printer = QPrinter() printer.setOutputFormat(QPrinter.PdfFormat) printer.setOrientation(QPrinter.Landscape) printer.setOutputFileName(fname) printer.setCreator("guidata") self.print_(printer) elif fname.lower().endswith(".png"): if PYQT5: pixmap = self.grab() else: pixmap = QPixmap.grabWidget(self) pixmap.save(fname, "PNG") else: raise RuntimeError(_("Unknown file extension"))
def setPaintAttribute(self, attribute, on=True): """ Changing the paint attributes Paint attributes: * `QwtPlotCanvas.BackingStore` * `QwtPlotCanvas.Opaque` * `QwtPlotCanvas.HackStyledBackground` * `QwtPlotCanvas.ImmediatePaint` :param int attribute: Paint attribute :param bool on: On/Off .. seealso:: :py:meth:`testPaintAttribute()`, :py:meth:`backingStore()` """ if bool(self.__data.paintAttributes & attribute) == on: return if on: self.__data.paintAttributes |= attribute else: self.__data.paintAttributes &= ~attribute if attribute == self.BackingStore: if on: if self.__data.backingStore is None: self.__data.backingStore = QPixmap() if self.isVisible(): if QT_MAJOR_VERSION >= 5: self.__data.backingStore = self.grab(self.rect()) else: self.__data.backingStore = QPixmap.grabWidget( self, self.rect()) else: self.__data.backingStore = None elif attribute == self.Opaque: if on: self.setAttribute(Qt.WA_OpaquePaintEvent, True) elif attribute in (self.HackStyledBackground, self.ImmediatePaint): pass