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}'")