I’ve got a script that currently is working, it grabs a screenshot of the active view but I am wanting to change that to allow the user to grab their own screenshot.
I got a python script working in Pycharm, but one python package is giving me issues.
The package is “pyautogui”.
I tried installing the same way as I installed other packages, but it still says “No module named 'pyautogui”.
This is my python script that I got working in Pycharm:
import pyautogui
import os
import tkinter as tk
# Function to select a region
def select_region():
global x_start, y_start, x_end, y_end, root, canvas
root = tk.Tk()
root.attributes('-fullscreen', True)
root.attributes('-alpha', 0.3)
root.configure(background='gray')
# Create a canvas for drawing the selection rectangle
canvas = tk.Canvas(root, cursor="cross", bg="gray", highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=True)
root.bind('<Button-1>', start_selection)
root.bind('<B1-Motion>', draw_selection)
root.bind('<ButtonRelease-1>', end_selection)
root.mainloop()
# Start region selection
def start_selection(event):
global x_start, y_start, rect
x_start, y_start = event.x, event.y
rect = canvas.create_rectangle(x_start, y_start, x_start, y_start, outline='red', width=2)
# Draw selection rectangle
def draw_selection(event):
global rect
canvas.coords(rect, x_start, y_start, event.x, event.y)
# End region selection
def end_selection(event):
global x_start, y_start, x_end, y_end, root
x_end, y_end = event.x, event.y
root.destroy()
def capture_region(save_path):
try:
print("Drag to select a region of your screen...")
select_region()
x1, y1, x2, y2 = min(x_start, x_end), min(y_start, y_end), max(x_start, x_end), max(y_start, y_end)
region = pyautogui.screenshot(region=(x1, y1, x2 - x1, y2 - y1))
region.save(save_path)
return f"Screenshot saved to {save_path}"
except Exception as e:
return f"Error: {str(e)}"
if __name__ == "__main__":
# Specify the save path on your desktop
save_path = r"C:\Users\ian\Desktop\test\screenshot.png"
# Ensure the directory exists
os.makedirs(os.path.dirname(save_path), exist_ok=True)
# Capture the region and save the screenshot
result = capture_region(save_path)
print(result)
Any suggestions on fixes or maybe a different pacakge to use for letting the user get a screeshot?