Created
April 22, 2021 21:13
-
-
Save israel-dryer/3efaa26b6a807c7af713e37980b1ea04 to your computer and use it in GitHub Desktop.
A class to attach to tkinter windows for taking screenshots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
from PIL import ImageGrab | |
class Screenshot: | |
def __init__(self, parent, filename): | |
self.parent = parent | |
self.parent.bind("<Insert>", self.get_bounding_box) | |
self.filename = filename | |
def get_bounding_box(self, event): | |
""" | |
Take a screenshot of the current demo window and save to images | |
""" | |
# bounding box | |
titlebar = 31 | |
x1 = self.parent.winfo_rootx() - 1 | |
y1 = self.parent.winfo_rooty() - titlebar | |
x2 = x1 + self.parent.winfo_width() + 2 | |
y2 = y1 + self.parent.winfo_height() + titlebar + 1 | |
self.parent.after_idle(self.save_screenshot, [x1, y1, x2, y2]) | |
def save_screenshot(self, bbox): | |
# screenshot | |
img = ImageGrab.grab(bbox=bbox) | |
# image name | |
img.save(self.filename, 'png') | |
print(self.filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment